用浏览器下载(图片/文件)到本地

来源:互联网 发布:果芽软件 编辑:程序博客网 时间:2024/05/22 03:43

1.pom.xml

<dependency>              <groupId>org.apache.poi</groupId>              <artifactId>poi</artifactId>              <version>3.9</version>          </dependency>  


2.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><c:set var="ctx" value="${pageContext.request.contextPath}" /><a href="${ctx }/aaa/ccc"> 点击下载</a>




3.controller

import java.io.File;import java.io.IOException;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.impay.utils.FileUtilsDownload;@Controller@RequestMapping("/aaa")public class A {@RequestMapping("/ccc")public void ccc(HttpServletResponse response) throws IOException{ String a="C:\\Users\\Administrator\\Desktop\\TEST2\\src\\main\\webapp\\KHtp\\mer_20170602171942359.jpg";//要下载的图片路径FileUtilsDownload.Download(new File(a), response);}//下载}


4.FileUtilsDownload

package com.impay.utils;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.http.HttpServletResponse;public  class FileUtilsDownload {/**      * 下载指定文件      *       * @param result      * @param response      * @throws IOException      */      public static void Download(File result, HttpServletResponse response)              throws IOException {          InputStream fis = new BufferedInputStream(new FileInputStream(result));          byte[] buffer = new byte[fis.available()];          fis.read(buffer);          fis.close();          response.reset(); // 清空输出流          response.setHeader("Content-disposition", "attachment;filename="                  + new String(result.getName().getBytes("GBK"), "ISO8859-1"));          response.setContentType("charset=UTF-8");// 定义输出类型          response.addHeader("Content-Length", "" + result.length());          OutputStream toClient = new BufferedOutputStream(                  response.getOutputStream());          response.setContentType("application/octet-stream");          toClient.write(buffer);          toClient.flush();          toClient.close();      } }


-----------------------------------------------------------------附---------------------------------------------------------------------------

import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.URL;import java.net.URLConnection;public class aaa {public static void main(String[] args) throws Exception {System.out.println(System.currentTimeMillis());String localUrl="D://"+System.currentTimeMillis()+".jpg";File file=new File(localUrl);if (!file.exists()) {          file.createNewFile();      }   /* download("http://172.16.120.222:9090/sms_boss/sms_check_file/merchant/sign_2017033010003636.jpg", "D://test1.jpg");*/download("https://www.baidu.com/img/bd_logo1.png", localUrl);}public static void download(String urlString, String filename) throws Exception {    // 构造URL    URL url = new URL(urlString);    // 打开连接    URLConnection con = url.openConnection();    // 输入流    InputStream is = con.getInputStream();    // 1K的数据缓冲    byte[] bs = new byte[1024];    // 读取到的数据长度    int len;    // 输出的文件流    OutputStream os = new FileOutputStream(new File(filename));    // 开始读取    while ((len = is.read(bs)) != -1) {      os.write(bs, 0, len);    }    // 完毕,关闭所有链接    os.close();    is.close();}   }




原创粉丝点击