我的代码应该通过多部分请求将带有少量参数的图像发送到 Web 服务器。参数发送没有问题,但图像显示为空图像文件,错误数据带有响应:{“name”:“1372429494726.jpg”,“type”:“”,“tmp_name”:“”,“错误":"无法上传照片。","size":0,"max_file_size":8}
可能是什么问题?谢谢
public boolean sendPicture(String photoUrl, String clientId, String rep){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
PrintWriter writer = null;
String urlServer = "http://myserver/v1/photos.json";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = Long.toHexString(System.currentTimeMillis());
;
String charset = "UTF-8";
int bytesRead, bytesAvailable, bufferSize;
int maxBufferSize = 1 * 1024 * 1024;
try{
File photoFile = new File(photoUrl);
FileInputStream fileInputStream = new FileInputStream(photoFile);
URL url = new URL(urlServer);
connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
// Send normal params.
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
writer.append("--" + boundary).append(lineEnd);
writer.append("Content-Disposition: form-data; name=\"rep\"").append(lineEnd);
writer.append("Content-Type: text/plain; charset=" + charset).append(lineEnd);
writer.append(lineEnd);
writer.append(rep).append(lineEnd).flush();
writer.append("--" + boundary).append(lineEnd);
writer.append("Content-Disposition: form-data; name=\"clientId\"").append(lineEnd);
writer.append("Content-Type: text/plain; charset=" + charset).append(lineEnd);
writer.append(lineEnd);
writer.append(clientId).append(lineEnd).flush();
// send image
writer.append("--" + boundary).append(lineEnd);
writer.append("Content-Disposition: form-data; name=\"source\"; filename=\"" + photoFile.getName() + "\"").append(lineEnd);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(photoFile.getName())).append(lineEnd);
writer.append("Content-Transfer-Encoding: binary").append(lineEnd);
writer.append(lineEnd).flush();
InputStream input = null;
try{
input = new FileInputStream(photoFile);
bytesAvailable = input.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
for(int length = 0; (length = input.read(buffer)) > 0;){
outputStream.write(buffer, 0, length);
}
outputStream.flush();
}finally{
if(input != null) try{
input.close();
}catch(IOException logOrIgnore){
}
}
writer.append(lineEnd).flush();
writer.append("--" + boundary + "--").append(lineEnd);
//read response
int serverResponseCode = connection.getResponseCode();
InputStream errorInput = connection.getErrorStream();
BufferedReader r = new BufferedReader(new InputStreamReader(errorInput));
StringBuilder total = new StringBuilder();
String line;
while((line = r.readLine()) != null){
total.append(line);
}
Log.v("error", total.toString());
Log.v("serverResponseCode", "" + serverResponseCode);
String serverResponseMessage = connection.getResponseMessage();
Log.v("serverResponseMessage", "" + serverResponseMessage);
fileInputStream.close();
outputStream.flush();
writer.close();
Log.v("uploaded photo", "" + photoUrl);
}catch(Exception ex){
Log.d("UploadPhotoTask", ex.getMessage());
}finally{
if(writer != null) writer.close();
}
return true;
}