1
こんにちは友人私は画像をアップロードするときに問題が発生します。無効なファイルですので、この問題を解決するためにお手伝いください。画像アップロードの問題(無効なファイルの応答)android
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());
}
}
In Invalid Flie Comes。私の問題を解決しようとします。
File fileToUpload = new File(filePath);
FileBody fileBody = new FileBody(fileToUpload, "image/jpeg");
reqEntity.addPart("file", fileBody);
の代わりByteArrayBody
を追加 - 画像を追加するには どうもありがとう
にしてみてください、あなたのエラーをログに記録するには、質問にスタックトレースとエラーメッセージを追加してください。 – Zds
エラーはありませんが、レスポンスは次のようになります。08-16 13:02:06.345:INFO/System.out(661):レスポンス:無効なファイル –
ur webserverに画像をアップロードできますか? –