C++設計形式之職責鏈形式。本站提示廣大學習愛好者:(C++設計形式之職責鏈形式)文章只能為提供參考,不一定能成為您想要的結果。以下是C++設計形式之職責鏈形式正文
本文實例為年夜家分享了Java辦事器中下載圖片的辦法,供年夜家參考,詳細內容以下
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.io.IOUtils;
/**
* 從辦事器中下載圖片
*
* @param fileName 圖片地址
* @param response
* @return
*/
@RequestMapping(value = "/download")
public void downloadMedia(HttpServletResponse response, HttpServletRequest request) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//處置中文亂碼
request.setCharacterEncoding("UTF-8");
String fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
//處置閱讀器兼容
response.setContentType("application/msexcel;charset=utf-8");//界說輸入類型
Enumeration enumeration = request.getHeaders("User-Agent");
String browserName = (String) enumeration.nextElement();
boolean isMSIE = browserName.contains("MSIE");
if (isMSIE) {
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF8"));
} else {
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
}
//url地址假如存在空格,會招致報錯! 處理辦法為:用+或許%20取代url參數中的空格。
fileName = fileName.replace(" ", "%20");
//圖片下載
URL url = new URL(fileName);
URLConnection conn = url.openConnection();
outputStream = response.getOutputStream();
inputStream = conn.getInputStream();
IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
System.err.println(e);
}finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。