嗨朋友,当我上传图片响应来到无效文件时我遇到问题所以请帮助解决这个问题。
public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MybitmapImage.compress(Bitmap.CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://myurl.net/uploadphoto.aspx");
ByteArrayBody bab = new ByteArrayBody(data,"5.png");
// File file= new File("/sdcard/mickey.jpg");
// FileBody bin = new FileBody(file,"image/jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// reqEntity.addPart("5.jpg",bin);
reqEntity.addPart("5.png", bab);
postRequest.setEntity(reqEntity);
System.out.println("Method"+postRequest.getMethod());
System.out.println("tostring"+postRequest.toString());
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
System.out.println("Error"+e);
Log.e(e.getClass().getName(), e.getMessage());
}
}
响应无效的Flie来了。所以请尽量解决我的问题。 非常感谢
答案 0 :(得分:0)
要添加图片,请尝试类似(您已注释的代码) -
File fileToUpload = new File(filePath);
FileBody fileBody = new FileBody(fileToUpload, "image/jpeg");
reqEntity.addPart("file", fileBody);
而不是添加ByteArrayBody
。
答案 1 :(得分:0)
要记录您的错误,请尝试使用catch
catch (Exception e) {
// handle exception here
e.printStackTrace();
}