1

我正在尝试将图像存储在远程位置。

BufferedImage img;
img=ImageIO.read(file);
String url="http://localhost:8080/prashant Pic/j.jpg";
ImageIO.write(img, "jpg",new File("/home/com/Documents/images/p.jpg"));
ImageIO.write(img, "jpg",new File("http://localhost:8080/prashant Pic/j.jpg"));

它将图像存储在本地计算机上。但不在本地主机上。

4

1 回答 1

0

完全可以将图像存储在远程位置。而不是使用File,使用类似的东西:

URL url = new URL("http://localhost:8080/prashant%20Pic/j.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);

// Do necessary setup of connection, like:
//  - setting the HTTP method (POST/PUT)
//  - setting authentication parameters?
// These depends on what kind of service you have running on localhost:8080

OutputStream stream = connection.getOutputStream();
try {
    ImageIO.write(img, "jpg", stream);
}
finally {
    stream.close();
}

显然,您还需要在 localhost:8080 上运行的服务,该服务接受通过 POST 或 PUT 上传的文件。

于 2013-07-16T07:54:39.390 回答