String filePath = "http://images.all-free-download.com/images
/graphiclarge/baby_child_girl_215819.jpg";
The above String is my download path.
URL url = new URL(filePath);
HttpURLConnection httpConn = (HttpURLConnection)
url.openConnection();
InputStream inStream = httpConn.getInputStream();
// if you want to use a relative path to context root:
@SuppressWarnings("unused")
String relativePath = getServletContext().getRealPath("");
// obtains ServletContext
ServletContext context = getServletContext();
// gets MIME type of the file
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
// modifies response
response.setContentType(mimeType);
response.setContentLength((int) httpConn.getContentLength());
String fileName = filePath.substring(filePath.lastIndexOf("/") +
1,
filePath.length());
// forces download
//String headerKey = "Content-Disposition";
//String headerValue = String.format("attachment;
filename=\"%s\"", fileName);
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition","attachment;
filename=\""+ fileName);
//response.setHeader(headerKey, headerValue);
// obtains response's output stream
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
以上代码适用于所有浏览器中的Android设备。但它不适用于iPhone。
点击下载按钮后,图像直接显示。不问任何想法。
我希望将图像保存到iphone中 任何人都可以帮助我。是否可以使用iphone。