*COMMENTS *
------------------
- I hide FTP details.
- All the code is inside a functioning and working task.
- serial = its a String that used here as the new folder name I create * a fixed name for each computer.
- imageName = its a String that stores the name of the image * Each time a different name is created.
What is the idea in this code snippet:
- Checks whether a serial folder exists on the server, then:
*Creates a server connection and upload a picture to a folder.
if not exists:
*Creates the folder and only then uploads the image to the server.
What is the problem:
In Code 1: Everything works like a clock, but the pictures even have size they "corrupt".
In Code 2: Everything works like a clock, but the pictures have no size they "corrupt" .
(for code 2) Here, I also tried a note I saw on the Internet trying to add (did not help *):
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
code 1:
FTPClient ftpClient = new FTPClient(); ftpClient.connect(host, port); ftpClient.login(username, pass); String clientFolder = "ftp://username:pass@host/ss/" + serial + "/" + imageName + ".jpg"; if(ftpClient.cwd("/ss/" + serial + "/") == 250) { ftpClient.changeWorkingDirectory("/ss/" + serial + "/"); System.out.println("your folder is located"); ftpInsertImage(image, clientFolder); } else if(ftpClient.cwd("/ss/" + serial + "/") == 550) { if(ftpClient.makeDirectory("/ss/" + serial)) { ftpClient.changeWorkingDirectory("/ss/" + serial + "/"); System.out.println("the folder was created successfully"); ftpInsertImage(image, clientFolder); } } } catch(Exception e) { e.printStackTrace(); } } public static void ftpInsertImage(BufferedImage image, String clientFolder) { BufferedImage img = image; String folder = clientFolder; try { URL url = new URL(folder); URLConnection con = url.openConnection(); ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); ImageIO.write(img, "jpg", out); System.out.println("finish to upload"); out.close(); } catch(Exception e) { e.printStackTrace(); } }
code 2:
File file = new File("./" + imageName + ".jpg"); if(ftpClient.cwd("/ss/" + serial + "/") == 250) { ftpClient.changeWorkingDirectory("/ss/" + serial + "/"); System.out.println("your folder is located"); ftpInsertImage(ftpClient, file, imageName); } else if(ftpClient.cwd("/ss/" + serial + "/") == 550) { if(ftpClient.makeDirectory("/ss/" + serial)) { ftpClient.changeWorkingDirectory("/ss/" + serial + "/"); System.out.println("the folder was created successfully"); ftpInsertImage(ftpClient, file, imageName); } } } catch(Exception e) { e.printStackTrace(); } } public static void ftpInsertImage(FTPClient ftpClient, File file, String ImageName) { File f = file; FTPClient client = ftpClient; String image = ImageName; try { InputStream input = new FileInputStream(f); System.out.println("start uploading the image"); if(client.storeFile(image + ".jpg", input)) { System.out.println("the image is uploaded successfully"); input.close(); client.logout(); client.disconnect(); } } catch(Exception e) { e.printStackTrace(); } }
I'm beginner then please don't have any complicated code