gpt4 book ai didi

java - 无法在 FTP 服务器上写入多个文件

转载 作者:行者123 更新时间:2023-12-02 10:40:18 25 4
gpt4 key购买 nike

您好,我在 OutputStream out = ftp.storeFileStream(path); 上遇到了诸如 java.lang.NullPointerException 之类的错误。当我写入一个文件后,无法继续在 FTP 服务器上写入第二个文件。

你能帮我一下吗?代码写了第一张图片并完全停止了写。这是我的方法代码........

    public void getIncidentPhotoByID(int incidentid, int photoId) {
String base64Image = null;
WebSSLClient client = new WebSSLClient();
String photo_Id= "incident_"+incidentid;
String jsonResponse,jsonResImg;
Response response =client.createRequest(PropertiesUtil.getOracleCloudRestUrL() + "/mobile/platform/storage/collections/dev_cre_incident_photos_collection/objects").get();
jsonResponse = response.readEntity(String.class);
Map<String,String> imgMap = new HashMap<>();
try {
JSONObject jsonObj = new JSONObject(jsonResponse);
JSONArray jsonArr = jsonObj.getJSONArray("items");
for (int i = 0; i < jsonArr.length(); ++i) {
JSONObject rec = jsonArr.getJSONObject(i);
String name = rec.getString("name");
String name_id = removeStr(name);
if(name_id.equals(photo_Id)){
Response response1 =client.createRequest(PropertiesUtil.getOracleCloudRestUrL() + "/mobile/platform/storage/collections/dev_cre_incident_photos_collection/objects/"+name).get();
jsonResImg = response1.readEntity(String.class);
imgMap.put(name, jsonResImg);
}
}
} catch (JSONException e) {
e.getMessage();
}

我将发布 2 个单独的代码。其实也是一种方法。

    FTPClient ftp = new FTPClient();  
FileInputStream fis = null;
try {
ftp.connect("link.myjpl.com");
ftp.login("user", "password");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
Set set;
set = imgMap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();

base64Image = mentry.getValue().toString();
System.out.println(base64Image);
String filename = mentry.getKey().toString();
System.out.println(filename);
String[] strings = base64Image.split(",");
String extension;
switch (strings[0]) {//check image's extension
case "data:image/jpeg;base64":
extension = "jpeg";
break;
case "data:image/png;base64":
extension = "png";
break;
default://should write cases for more images types
extension = "jpg";
break;
}
byte[] data1 = Base64.decodeBase64(strings[1]);
String dirPath ="ReportImages/test/";
String dir = dirPath+photo_Id;
boolean dirExists = ftp.changeWorkingDirectory(dir);
if(!dirExists){
ftp.makeDirectory(dir);
}
String path = dir+"/"+filename+"."+ extension;

OutputStream out = ftp.storeFileStream(path);
out.write(data1);
ftp.completePendingCommand();
out.flush();
}
ftp.disconnect();
} catch (IOException e) {
System.out.print(e);
e.printStackTrace();
System.out.println("Error while FTP'ing ");
}
}

这是 NPE 错误!

SEVERE: 

java.lang.NullPointerException 在 com.scm.cre.cloud.model.services.CareCloudDbAppModuleImpl.getIncidentPhotoByID(CareCloudDbAppModuleImpl.java:144) 在 com.scm.cre.batch.job.ConnectTest.callRestAip(ConnectTest.java:20) 在 com.scm.cre.batch.CareExecutor.main(CareExecutor.java:84)

进程已退出,退出代码为 1。这是一个屏幕截图...... enter image description here

最佳答案

看看javadocs

它说:

如果无法打开数据连接(例如文件不存在),则返回null(此时可以检查回复码以确定失败的具体原因)。

因此你的代码应该这样做:

if (out == null) {
System.out.println(ftp.getReplyCode());
System.out.println (ftp.getReplyString());
}

就我而言,原因是

550 权限被拒绝。

就我而言,可以通过编辑/etc/vsftpd.conf 来解决

并更改为

write_enable=YES

保存文件并使用 sudo service vsftpd restart 重新启动 vsftpd。

这里是一个更完整的示例

FTPClient ftp = new FTPClient();

try {
ftp.connect("somehost");
ftp.login("user", "pw");
ftp.enterLocalPassiveMode();

OutputStream out = ftp.storeFileStream("dump1");
if (out == null) {
System.out.println(ftp.getReplyCode());
System.out.println (ftp.getReplyString());
return;
}
out.write("test is a test".getBytes());
out.flush();
out.close();
System.out.println (ftp.getReplyString());
ftp.completePendingCommand();
System.out.println (ftp.getReplyString());

out = ftp.storeFileStream("dump2");
if (out == null) {
System.out.println(ftp.getReplyCode());
System.out.println (ftp.getReplyString());
return;
}
out.write("test is a test2".getBytes());
out.flush();
out.close();
System.out.println (ftp.getReplyString());
ftp.completePendingCommand();
System.out.println (ftp.getReplyString());

} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
ftp.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于java - 无法在 FTP 服务器上写入多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52963235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com