| | |
| | | package com.whyc.util; |
| | | |
| | | import java.io.File; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | |
| | | return list; |
| | | } |
| | | |
| | | public static Boolean deleteFile(File file) { |
| | | //判断文件不为null或文件目录存在 |
| | | if (file == null || !file.exists()) { |
| | | System.out.println("文件删除失败,请检查文件是否存在以及文件路径是否正确"); |
| | | return false; |
| | | } |
| | | //获取目录下子文件 |
| | | File[] files = file.listFiles(); |
| | | //遍历该目录下的文件对象 |
| | | for (File f : files) { |
| | | //判断子目录是否存在子目录,如果是文件则删除 |
| | | if (f.isDirectory()) { |
| | | //递归删除目录下的文件 |
| | | deleteFile(f); |
| | | } else { |
| | | //文件删除 |
| | | f.delete(); |
| | | //打印文件名 |
| | | } |
| | | } |
| | | //文件夹删除 |
| | | file.delete(); |
| | | return true; |
| | | } |
| | | |
| | | public static void download(HttpServletResponse resp,String inFilePath,String outFileFullName){ |
| | | try { |
| | | // 转码防止乱码 |
| | | //resp.addHeader("Content-Disposition", "attachment;filename=" + new String(softwareName.getBytes("UTF-8"), "ISO8859-1")); |
| | | resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode ( outFileFullName, "utf-8")); |
| | | OutputStream out = resp.getOutputStream(); |
| | | FileInputStream in = new FileInputStream(inFilePath); |
| | | int len=0; |
| | | byte[] buffer =new byte[1024]; |
| | | //7. 将缓冲区中的数据输出 |
| | | while ((len=in.read(buffer))>0){ |
| | | out.write(buffer,0,len); |
| | | } |
| | | in.close(); |
| | | out.close(); |
| | | } catch (FileNotFoundException | UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static String saveFile(MultipartFile multipartFile,String fileName) throws IOException { |
| | | String rootFile = CommonUtil.getRootFile(); |
| | | |
| | | String filePath = rootFile + fileName; |
| | | File file = new File(filePath); |
| | | File parentFile = file.getParentFile(); |
| | | if(!parentFile.exists()){ |
| | | parentFile.mkdirs(); |
| | | } |
| | | //存储 |
| | | multipartFile.transferTo(file); |
| | | |
| | | return "doc_file"+fileName; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | File file = new File("C:\\Users\\29550\\Desktop\\当前项目\\202207泰州平台"); |
| | | List list = new ArrayList<>(); |