你好,我在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
答案 0 :(得分:1)
看看javadocs
它说:
如果无法打开数据连接(例如,文件不存在),则返回null(在这种情况下,您可以检查回复代码以确定失败的确切原因)。 < / p>
因此您的代码应该执行以下操作:
if (out == null) {
System.out.println(ftp.getReplyCode());
System.out.println (ftp.getReplyString());
}
在我看来,原因是
550权限被拒绝。
在我的情况下,可以通过编辑/etc/vsftpd.conf来治愈
并更改为
write_enable = YES
保存文件并使用sudo服务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();
}
}