文件相关操作

您所在的位置:网站首页 java循环删除文件 文件相关操作

文件相关操作

#文件相关操作 | 来源: 网络整理| 查看: 265

文件下载/* * 下载文件 */ public void downloadFile(String proname, String fileName, HttpServletResponse resp) { try { ProjectEntity projectEntity = projectMapping.getProjectByName(proname); ProductEntity productEntity = productMapping.getProductByProductId(projectEntity.getProProductId()); ProductOutsourceEntity productOutsourceEntity = productOutsourceMapping.getProductOutsourceByOutId(productEntity.getOutId()); //获取文件名 String strUrl = productOutsourceEntity.getFileUrl(); String filename = strUrl.substring(strUrl.lastIndexOf("/")+1); filename = new String(filename.getBytes("iso8859-1"),"UTF-8"); String path = strUrl; File file = new File(path); //如果文件不存在 if(!file.exists()){ log.info("下载文件不存在"); } //解决下载文件时文件名乱码问题 byte[] fileNameBytes = filename.getBytes(StandardCharsets.UTF_8); filename = new String(fileNameBytes, 0, fileNameBytes.length, StandardCharsets.ISO_8859_1); resp.reset(); resp.setContentType("application/octet-stream"); resp.setCharacterEncoding("utf-8"); resp.setContentLength((int) file.length()); //设置响应头,控制浏览器下载该文件 resp.setHeader("content-disposition", "attachment;filename=" + filename); try{ //读取要下载的文件,保存到文件输入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); //创建输出流 OutputStream os = resp.getOutputStream(); //缓存区 byte[] buff = new byte[1024]; int i = 0; //循环将输入流中的内容读取到缓冲区中 while ((i = bis.read(buff)) != -1) { os.write(buff, 0, i); os.flush(); } //关闭 bis.close(); os.close(); } catch (IOException e) { log.error("{}",e); log.info("下载失败"); } log.info("下载成功"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } 文件夹下载

与文件下载不一样,需要先压缩再下载。本例子的服务器中文件URL已知,所以未用mapping层查询数据库,可根据需要加上。

Service:/* * 下载文件夹 */ public void downloadFile(String proname, String fileName, HttpServletResponse resp) { try { //下载后的文件 String name=Constants.FILEPATH + Constants.PROJECT + proname + "/product/outsource"+".zip"; zipUtil zc = new zipUtil(name); List list=new ArrayList(); //服务器存储的URL list.add(Constants.FILEPATH + Constants.PROJECT + proname + "/product/outsource"); //可继续添加要一起压缩的文件夹或文件 //list.add(""); String[] strings = new String[list.size()]; list.toArray(strings); File file=null; try { zc.compress(strings);//压缩 file=new File(name); downLoad.downloadZip(file,resp);//下载 } catch (IOException e) { e.printStackTrace(); }finally { if(file!=null){ file.delete();//把生成的压缩文件删除 } } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } Utile:

建立两个工具包

(1)压缩文件夹

public class ZipCompressorUtil { static final int BUFFER = 8192; /** * 压缩的文件夹 */ private File zipFile1111; public ZipCompressorUtil(String pathName) { zipFile1111 = new File(pathName); } /** * 遍历需要压缩文件集合 * @param pathName * @throws IOException */ public void compress(String... pathName) throws IOException { ZipOutputStream out =null; FileOutputStream fileOutputStream=null; CheckedOutputStream cos=null; try { fileOutputStream = new FileOutputStream(zipFile1111); cos = new CheckedOutputStream(fileOutputStream,new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; for (int i=0;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3