您好我正在尝试使用 ftp4j 将图像上传到服务器。我的图像已上传到服务器但已损坏。我知道我的代码中缺少某些内容。我的代码是
private class UploadBackgroundTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
dialog = new ProgressDialog(xxx.this);
dialog.setMessage("Loading");
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(final String... args) {
try{
TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManager, new SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
FTPClient client = new FTPClient();
client.setSSLSocketFactory(sslSocketFactory);
client.setSecurity(FTPClient.SECURITY_FTPES);
client.connect("xxx.xxx.com");
client.login("xxx", "xxx");
client.changeDirectory("product_images/import");
client.setType(FTPClient.TYPE_BINARY);
//@TODO read file from android system
System.out.println("temPath -->"+picturepath);
client.upload(new File(picturepath));
FTPFile[] list = client.list();
for(int i=0;i<list.length;i++){
System.out.println("-->"+list[i]);
}
Log.v("SUCCESFULLY UPLOADED", "SUCCESFULLY UPLOADED");
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
protected void onPostExecute(String list) {
dialog.dismiss();
}
}
我在服务器上的图像是:
我的日志跟踪是:
it.sauronsoftware.ftp4j.FTPException [code=450, message= Transfer aborted. Link to file server lost]
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2799)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2586)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2495)
at com.Activity.xxx.AddProducts$UploadBackgroundTask.doInBackground(AddProducts.java:902)
at com.Activity.xxx.AddProducts$UploadBackgroundTask.doInBackground(AddProducts.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Window already focused, ignoring focus gain of:
com.android.internal.view.IInputMethodClient$Stub$Proxy@41174c80
上传后服务器上的文件大小始终为 4 KB。为什么?
我搜索了很多但没有得到答案。请告诉我我的代码有什么问题。提前致谢。