瀏覽器下載圖片

/**
     * 下載圖片
     * @param request
     * @throws IOException 
     */
    @ResponseBody
    @RequestMapping("downLoad.do")
    public void downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	String url = request.getParameter("pictureUrl");
    	String picName = url.substring(url.lastIndexOf("/")+1);
    	String[] split = picName.split("\\.");
    	//獲取文件
    	File file = new File(url);
    	//將圖片讀成二進制流
    	FileImageInputStream  fs = new FileImageInputStream (file);
    	int streamLength = (int)fs.length();
    	byte[] image = new byte[streamLength ];
    	fs.read(image,0,streamLength );
    	fs.close();
    	response.setHeader("Content-Type","application/octet-stream");
    	response.setHeader("Content-Disposition","attachment;filename="+split[0]+"."+split[1]);
    	response.getOutputStream().write(image);
    	response.getOutputStream().flush();
    	response.getOutputStream().close();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章