我的代码应该将带有少量参数的图像发送到具有多部分请求的Web服务器。参数发送没有问题,但图像作为空图像文件和错误数据出现响应:{“name”:“1372429494726.jpg”,“type”:“”,“tmp_name”:“”,“错误“:”无法上传照片。“,”尺寸“: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;
}
答案 0 :(得分:0)
管理解决它:
public boolean sendPicture(String photoUrl, String clientId, String rep){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = photoUrl;
String urlServer = "http://myserver/v1/photos.json";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
int serverResponseCode = 0;
try{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection)url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"source\";filename=\"" + pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while(bytesRead > 0){
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"clientId\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(clientId + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"rep\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(rep + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
Log.v("serverResponseCode", "" + serverResponseCode);
String serverResponseMessage = connection.getResponseMessage();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = in.readLine()) != null)
Log.d("server", line);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}catch(Exception ex){
Log.d("UploadPhotoTask", ex.getMessage());
return false;
// Toast.makeText(this, "Error: " + ex.getMessage(),
// Toast.LENGTH_LONG).show();
}
if(serverResponseCode == 200) return true;
else return false;
}