用浏览器下载(图片/文件)到本地
来源:互联网 发布:果芽软件 编辑:程序博客网 时间: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();} }
阅读全文
0 0
- 用浏览器下载(图片/文件)到本地
- 服务器通过浏览器下载文件到本地
- C#下在图片文件到本地,c#,c#下载,下载图片,下载文件,下载函数
- 浏览器自动下载文件到本地,JavaScript中的ActiveXObject对象
- 关于Ajax无法下载文件到浏览器本地的问题
- 浏览器客户端从服务器下载文件到本地
- 网络资源(图片)下载到本地
- 下载文件到本地
- 使用URLConnection下载文件或图片并保存到本地
- 使用urlconnection下载文件或图片并保存到本地
- 使用urlconnection下载文件或图片并保存到本地
- 移动端下载文件或图片(h5+的Downloader下载网络图片缓存到本地的案例)
- java 下载图片到本地
- C# 下载图片到本地
- java下载图片到本地
- 下载网络图片到本地
- js下载图片到本地
- python下载图片到本地
- 苹果开发几个常见的错误
- Linux用户查看系统有多少用户在登录
- 1052. Linked List Sorting (25)
- PHP设计模式系列
- Objective-C学习笔记---构造函数和析构函数
- 用浏览器下载(图片/文件)到本地
- mysql在开发中的优化技巧和慢查询日志的配置
- Atcoder ABC 061D Score Attack 最长路+判环
- Objective-C
- MacBook Air 使用技巧
- String.format() 格式化字符串
- 设计模式之策略模式
- android 回调函数一:基本概念
- android 回调函数二:应用实例