Hello I have got error such as java.lang.NullPointerException on OutputStream out = ftp.storeFileStream(path);. after I write one file and was not able to continue to write second files on FTP server.
Could you please help me? the code wrote 1st image and stopped writing it at all. Here is my method code ........
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();
}
I will post 2 separate codes. It's one method in fact.
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 ");
}
}
Here is NPE error!
SEVERE:
java.lang.NullPointerException at com.scm.cre.cloud.model.services.CareCloudDbAppModuleImpl.getIncidentPhotoByID(CareCloudDbAppModuleImpl.java:144) at com.scm.cre.batch.job.ConnectTest.callRestAip(ConnectTest.java:20) at com.scm.cre.batch.CareExecutor.main(CareExecutor.java:84)
Process exited with exit code 1. Here is a screen shot.... enter image description here
Having a look at the javadocs
it says:
If the data connection cannot be opened (e.g., the file does not exist), null is returned (in which case you may check the reply code to determine the exact reason for failure).
Therefore your code should do:
In my case the reason is
550 Permission denied.
which in my case was curable by editing /etc/vsftpd.conf
and changing to
write_enable=YES
Save the file and restart vsftpd with sudo service vsftpd restart.
here is a fuller example