我正在尝试将图像上传到我的服务器,然后在iOS中将其下载到应用程序中。当我使用java上传/下载时,它可以正常工作,但是当我在iOS中下载时,图像会变黑。此外,当我在浏览器中查看图像时,它上面有紫色色调,使我确信图像已损坏。我没有正确上传图片吗?
代码:
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紫色色调:
iOS代码:
- (void) refreshImage {
client.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://link.jpg"]]]; ;
}