1、js代碼:html
代碼以下:java
/**
* 點擊下載當前圖片
*
*/
function downloadThisImage(obj){
var tid = $(obj).attr("file_tid");
var fileSrc = $(obj).parent().prev().attr("src");
window.location.href='../../file/toDownloadHmImage.do?tid='+tid+'&fileSrc='+fileSrc;
}瀏覽器
2、圖片下載的公共方法
package com.zxct.edu.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletResponse;
public class ImagesUtil {
public static void download2(HttpServletResponse response,String urlString, String filename) throws Exception {
//new一個URL對象
URL url = new URL(urlString);
//打開連接
URLConnection conn = url.openConnection();
//超時響應時間爲5秒
conn.setConnectTimeout(5 * 1000);
//經過輸入流獲取圖片數據
InputStream inStream = conn.getInputStream();
//獲得圖片的二進制數據,以二進制封裝獲得數據,具備通用性
byte[] data = readInputStream(inStream);
//new一個文件對象用來保存圖片,默認保存當前工程根目錄
//File imageFile = new File(filename);
//建立輸出流
OutputStream outStream = response.getOutputStream();
//寫入數據
outStream.write(data);
//關閉輸出流
outStream.close();
}
public static void download(HttpServletResponse response,String urlString, String filename) throws Exception {
// 構造URL
URL url = new URL(urlString);
// 打開鏈接
URLConnection con = url.openConnection();
//超時響應時間爲10秒
con.setConnectTimeout(10 * 1000);
// 輸入流
InputStream is = con.getInputStream();
// 1K的數據緩衝
byte[] bs = new byte[1024];
// 讀取到的數據長度
int len;
// 輸出的文件流
OutputStream os =response.getOutputStream();
// 開始讀取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完畢,關閉全部連接
os.close();
is.close();
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//建立一個Buffer字符串
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,若是爲-1,表明所有讀取完畢
int len = 0;
//使用一個輸入流從buffer裏把數據讀取出來
while( (len=inStream.read(buffer)) != -1 ){
//用輸出流往buffer裏寫入數據,中間參數表明從哪一個位置開始讀,len表明讀取的長度
outStream.write(buffer, 0, len);
}
//關閉輸入流
inStream.close();
//把outStream裏的數據寫入內存
return outStream.toByteArray();
}
}
注:download和download2方法均能實現下載。
3、Controller接口方法:app
/**
* 下載圖片文件
* @param request
* @param response
* @param model
* @return
* @throws ServletException
* @throws IOException
* @author zhangsq
*/
@RequestMapping("/toDownloadHmImage.do")
public void toDownloadHmImage(HttpServletRequest request, HttpServletResponse response,
ModelMap model,String fileSrc,String tid) throws ServletException, IOException {
String filePath = fileSrc.substring(0,fileSrc.indexOf("?"));
String realname = filePath.substring(filePath.lastIndexOf("/")+1);
// 設置響應頭,控制瀏覽器下載該文件
response.reset();
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
try {
ImagesUtil.download(response,filePath, realname);
} catch (Exception e) {
e.printStackTrace();
ServletOutputStream sos = null;
try {
response.setContentType("text/html;charset=utf-8");
sos = response.getOutputStream();
sos.write("<script>alert('下載失敗,請與管理員聯繫~~');</script>".getBytes());
sos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}finally{
if(null != sos){
try {
sos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}url