0

I am attempting to upload an image to my server in java and then download it in iOS within an app. When I upload / download with java, it works, but when I download it in iOS the image just turns up black. Also, when I view the image in browser it has a purple hue over it convincing me that the image is corrupt. Am I not uploading the image correctly?

Code:

public void uploadToServer() {

    String server = "ip";
    int port = 21;
    String user = "name";
    String pass = "pass";

    FTPClient ftpClient = new FTPClient();

    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        int w = 765;
        int h = 503;
        double d = .85;
        BufferedImage img = resize(client.methods.getClientImage(),
                (int) (w * d), (int) (h * d));

        ImageIO.write(img, "JPG", os);
        InputStream inputStream = new     ByteArrayInputStream(os.toByteArray());

        String secondRemoteFile = "directory";

        // System.out.println("Start uploading file");
        OutputStream outputStream = ftpClient
                .storeFileStream(secondRemoteFile);

        byte[] bytesIn = new byte[4096];
        int read = 0;

        while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
        }

        inputStream.close();
        outputStream.close();
        ftpClient.completePendingCommand();

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Heres the purple tint: enter image description here

iOS Code:

- (void) refreshImage {
 client.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:    [NSURL   URLWithString:@"http://link.jpg"]]]; ;
}
Dan
  • 47
  • 5
  • This code looks good. Try to download image manually from server and verify. You can simplify by following http://stackoverflow.com/questions/43157/easy-way-to-write-contents-of-a-java-inputstream-to-an-outputstream – kamoor Jan 17 '15 at 03:25
  • It does indeed work when being downloaded within another java application, it does not work within an iOS application though. – Dan Jan 17 '15 at 03:27
  • Where's the iOS code? –  Jan 17 '15 at 03:36
  • Then it is iOS code issue, please provide those – kamoor Jan 17 '15 at 03:37
  • Ok I posted iOS code, and I've also discovered that it works if I use a PNG file instead of JPG but I need to use JPG for it is compressed more efficiently and I need to download the image as fast as possible. – Dan Jan 17 '15 at 04:00
  • 1
    The problem might be what you do before uploading the file onto a FTP server. Your image compression method code in Java might not be compatible with iOS or just doesn't work as expected. Please try to remove image compression code around "BufferedImage img = resize(client.methods.getClientImage(), (int) (w * d), (int) (h * d)); ImageIO.write(img, "JPG", os);" and directly verify the image by downloading the file with FTP in a desktop browser or Safari on iPhone without your iOS app. – kcome Jan 17 '15 at 04:20
  • I tried it without the resize and got the same result – Dan Jan 17 '15 at 05:05

0 Answers0