JavaWeb 实现使⽤浏览器从服务器下载⽂件
Java Web实现 使⽤浏览器从服务器下载⽂件。
代码实现:
package ;import SneakyThrows ;import HttpServletRequest ;import HttpServletResponse ;import File ;import FileInputStream ;import IOException ;import OutputStream ;import URLEncoder ;public class FileUtile {/**⽂件下载并且浏览器导出**/ @SneakyThrows public static void downloadFile (String file , String fileName , HttpServletResponse response , HttpServletRequest request ) { OutputStream out = null ; FileInputStream in = null ; try { // 1.读取要下载的内容 in = new FileInputStream (new File (file )); // 2. 告诉浏览器下载的⽅式以及⼀些设置 // 解决⽂件名乱码问题,获取浏览器类型,转换对应⽂件名编码格式,IE 要求⽂件名必须是utf-8, firefo 要求是iso-8859-1编码 String agent = request .getHeader ("user-agent"); if (agent .contains ("FireFox")) { fileName = new String (file .getBytes ("UTF-8"), "iso-8859-1"); } else {
fileName = URLEncoder .encode (fileName , "UTF-8"); } // 设置下载⽂件的mineType ,告诉浏览器下载⽂件类型 String mineType = request .getServletContext ().getMimeType (fileName ); response .setContentType (mineType ); // 设置⼀个响应头,⽆论是否被浏览器解析,都下载 response .setHeader ("Content-disposition", "attachment; filename=" + fileName ); // 将要下载的⽂件内容通过输出流写到浏览器 out = response .getOutputStream (); int len = 0; byte [] buffer = new byte [1024]; while ((len = in .read (buffer )) > 0) { out .write (buffer , 0, len ); } deleteFile (file ); } catch ( IOException e ) { e .printStackTrace (); } finally { if (out != null ) { out .close (); } if (in != null ) { in .close (); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15want是什么意思
16
17
javascript 教程 百度云18
19
20
21
22
23
简述数据库的特点24
25
26
27
28
29
30
31
32
shell脚本写计时脚本33
34
35
36
37
38
39
40
41
42
43表单提交源码
44
45
46
47
48
49java下载过程
50
51
52
53
54
55
56
57
58
/** * 删除单个⽂件 * * @param fileName 要删除的⽂件的⽂件名 * @return 单个⽂件删除成功返回true ,否则返回false */ public static boolean deleteFile (String fileName ) { File file = new File (fileName ); // 如果⽂件路径所对应的⽂件存在,并且是⼀个⽂件,则直接删除 if (fi
le .exists () && file .isFile ()) { if (file .delete ()) { System .out .println ("删除单个⽂件" + fileName + "成功!"); return true ; } else { System .out .println ("删除单个⽂件" + fileName + "失败!"); return false ; } } else { System .out .println ("删除单个⽂件失败:" + fileName + "不存在!"); return false ; } }/**检查导出⽂件**/ public String examineExportFile (String title , String [] header , List <FlightActivityVo > list ) throws IOException { String pathString = this .getPathString (); String pathName = pathString + File .separator + title ; log .info ("*******************************pathName==================>" + pathName ); File file = new File (pathName ); FileOutputStream fos = new FileOutputStream (file ); OutputStreamWriter osw = new OutputStreamWriter (fos , "GBK"); CSVFormat csvFormat = CSVFormat .DEFAULT .withHeader (header ); CSVPrinter csvPrinter = new CSVPrinter (osw , csvFormat ); for (FlightActivityVo flightActivityVo : list ) { csvPrinter .printRecord (flightActivityVo .getOrigination (), flightActivityVo .getDestination (),flightActivityVo .getSegmentDisNumber (), flightActivityVo .getSegmentNumber (), flightActivityVo .getIntegralNumber (), flightActivityVo .getIntegralMax ()); } csvPrinter .flush (); csvPrinter .close (); log .info ("导出⽂件创建成功"); return pathName ; }/**获取⽂件路径**/ public static String getPathString () { String path = PathUtil .getRootPath (); File file = new
File (path ); File parentFile = file .getParentFile ().getParentFile ().getParentFile (); String pathString = ""; if (parentFile .toString ().startsWith ("file:")) { pathString = parentFile .toString ().substring (5, parentFile .toString ().length ()); } else { pathString = parentFile .toString (); } log .info ("*******************************pathString==================>" + pathString ); return pathString ; }/**⽂件压缩**/
5960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
/**⽂件压缩**/ public static String zipCsv (String fileName ) { //定义zip 压缩包 ZipOutputStream zos = null ; InputStream fis = null ; String pathString = getPathString (); String result = ""; try { String zipName = "数据导出" + System .currentTimeMillis () + ".zip"; zos = new ZipOutputStream (new FileOutputStream (pathString + zipName )); fis = new FileInputStream (new String ((pathString + File .separator + fileName ).getBytes (StandardCharsets .UTF_8),StandardCharsets .UTF_8)); ZipEntry z1 = new ZipEntry (fileName ); zos .putNextEntry (z1); byte [] b = new byte [1024]; int leng = 0; while ((leng = fis .read (b )) != -1) { zos .write (b , 0, leng ); } fis .close (); zos .close (); } catch (Exception e ) { e .printStackTrace (); } finall
y { if (zos != null ) { try { zos .close (); } catch (IOException e ) { e .printStackTrace (); } } if (fis != null ) { try { fis .close (); } catch (IOException e ) { e .printStackTrace (); } } } return zipName ; } }124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论