| | |
| | | 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 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 copyDirectory(File source, File destination) { |
| | | if (source.isDirectory()) { |
| | | if (!destination.exists()) { |
| | | destination.mkdir(); |
| | | } |
| | | for (File file : source.listFiles()) { |
| | | copyDirectory(file, new File(destination, file.getName())); |
| | | } |
| | | } else { |
| | | try (FileInputStream inputStream = new FileInputStream(source); |
| | | FileOutputStream outputStream = new FileOutputStream(destination)) { |
| | | byte[] buffer = new byte[1024]; |
| | | int length; |
| | | while ((length = inputStream.read(buffer)) > 0) { |
| | | outputStream.write(buffer, 0, length); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | File file = new File("C:\\Users\\29550\\Desktop\\当前项目\\202207泰州平台"); |
| | | List list = new ArrayList<>(); |