我正在嘗試編寫一個Android應用程序,它接收用戶圖像並將其上載到服務器。我可以在手機上查看圖像,但從服務器複製文件時無法查看圖像。我正在使用Apache公共庫來做FTP上傳。該文件出現,大小正確,但無法打開。發送到服務器後JPEG損壞
public static void uploadImage(Bitmap bitmap,String path)
{
String filePath="RENT/images/capture/TESTFILE.jpg";
try
{
FTPClient client = new FTPClient();
client.connect("MY IP");
client.login("USER", "PWD");
client.enterLocalPassiveMode();
Log.i("aaa","connected: "+client.isConnected());
Log.i("aaa","going to addr: "+client.getRemoteAddress());
FileInputStream fis = new FileInputStream(bitmapToFile(bitmap));
Log.i("aaa","2-starting to upload file");
client.storeFile(filePath, fis);
fis.close();
client.logout();
Log.i("aaa","3-file upload complete");
}
catch (IOException e) {
e.printStackTrace();
}
}
public static File bitmapToFile(Bitmap bitmap) throws IOException
{
File outputDir = con.getCacheDir();
File outputFile = File.createTempFile("testFile", ".jpg", outputDir);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 90, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(outputFile);
fos.write(bitmapdata);
fos.flush();
fos.close();
return outputFile;
}
}
請參考http://stackoverflow.com/a/8803503/1915697它 –
可能重複的[如何將圖像上傳到服務器在Android?](http://stackoverflow.com/questions/8803430/how-to- android.writeBytes(「Content-Disposition:form-data; name =」uploaded_file「; filename =」「+ fileName +」「」+ upload-image-into-server-in-android) –