Spring中如何实现MultipartFile和File之间互相转换呢?
下文笔者讲述Spring中File对象和MultpartFile对象互转的方法说明,如下所示
File转MultipartFile
方式1: 使用org.springframework.mock.web.MockMultipartFile进行转换 这种方式需导入spring-test.jar 方式2: 借助CommonsMultipartFile对象进行转换例
使用org.springframework.mock.web.MockMultipartFile
public static void main(String[] args) throws Exception { String filePath = "D:\\test.txt"; File file = new File(filePath); FileInputStream fileInputStream = new FileInputStream(file); // MockMultipartFile(String name, @Nullable String originalFilename, // @Nullable String contentType, InputStream contentStream) // 其中originalFilename,String contentType 旧名字,类型 可为空 // ContentType.APPLICATION_OCTET_STREAM.toString() 需要使用HttpClient的包 MultipartFile multipartFile = new MockMultipartFile("copy"+file.getName(), file.getName(),ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream); System.out.println(multipartFile.getName()); // 输出copytest.txt }
使用CommonsMultipartFile
public static void main(String[] args) throws Exception { String filePath = "D:\\test.txt"; File file = new File(filePath); // 需要导入commons-fileupload的包 FileItem fileItem = new DiskFileItem("copyfile.txt", Files.probeContentType(file.toPath()),false,file.getName(),(int)file.length(),file.getParentFile()); byte[] buffer = new byte[4096]; int n; try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){ while ( (n = inputStream.read(buffer,0,4096)) != -1){ os.write(buffer,0,n); } //也可以用IOUtils.copy(inputStream,os); MultipartFile multipartFile = new CommonsMultipartFile(fileItem); System.out.println(multipartFile.getName()); }catch (IOException e){ e.printStackTrace(); } }
MultipartFile转File
实现思路: 方式1: File转MultipartFile的逆过程 方式2: 使用transferTo进行转换
public static void main(String[] args) throws Exception { int n; // 得到MultipartFile文件 MultipartFile multipartFile = getFile(); File f = null; // 输出文件的新name 就是指上传后的文件名称 System.out.println("getName:"+multipartFile.getName()); // 输出源文件名称 就是指上传前的文件名称 System.out.println("Oriname:"+multipartFile.getOriginalFilename()); // 创建文件 f = new File(multipartFile.getOriginalFilename()); try ( InputStream in = multipartFile.getInputStream(); OutputStream os = new FileOutputStream(f)){ // 得到文件流。以文件流的方式输出到新文件 // 可以使用byte[] ss = multipartFile.getBytes();代替while byte[] buffer = new byte[4096]; while ((n = in.read(buffer,0,4096)) != -1){ os.write(buffer,0,n); } // 读取文件第一行 BufferedReader bufferedReader = new BufferedReader(new FileReader(f)); System.out.println(bufferedReader.readLine()); // 输出路径 bufferedReader.close(); }catch (IOException e){ e.printStackTrace(); } // 输出file的URL System.out.println(f.toURI().toURL().toString()); // 输出文件的绝对路径 System.out.println(f.getAbsolutePath()); // 操作完上的文件 需要删除在根目录下生成的文件 File file = new File(f.toURI()); if (file.delete()){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } } /** * * @Description 返回MultipartFile文件 * @return org.springframework.web.multipart.MultipartFile */ public static MultipartFile getFile() throws IOException { String filePath = "D:\\test.txt"; File file = new File(filePath); FileItem fileItem = new DiskFileItem("copyfile.txt", Files.probeContentType(file.toPath()),false,file.getName(),(int)file.length(),file.getParentFile()); byte[] buffer = new byte[4096]; int n; try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){ while ( (n = inputStream.read(buffer,0,4096)) != -1){ os.write(buffer,0,n); } //也可以用IOUtils.copy(inputStream,os); MultipartFile multipartFile = new CommonsMultipartFile(fileItem); System.out.println(multipartFile.getName()); return multipartFile; }catch (IOException e){ e.printStackTrace(); } return null; }
使用transferTo
public static void main(String[] args) throws Exception { String path = "D:\\test\\"; File file = new File(path,"demo.txt"); // 得到MultipartFile文件 MultipartFile multipartFile = getFile(); /*byte[] ss = multipartFile.getBytes(); OutputStream os = new FileOutputStream(file); os.write(ss); os.close();*/ multipartFile.transferTo(file); // 读取文件第一行 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); System.out.println(bufferedReader.readLine()); // 输出绝对路径 System.out.println(file.getAbsolutePath()); bufferedReader.close(); // 操作完上的文件 需要删除在根目录下生成的文件 if (file.delete()){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } } /** * * @Description 返回MultipartFile文件 * @return org.springframework.web.multipart.MultipartFile */ public static MultipartFile getFile() throws IOException { String filePath = "D:\\test.txt"; File file = new File(filePath); FileItem fileItem = new DiskFileItem("copyfile.txt", Files.probeContentType(file.toPath()),false,file.getName(),(int)file.length(),file.getParentFile()); byte[] buffer = new byte[4096]; int n; try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){ while ( (n = inputStream.read(buffer,0,4096)) != -1){ os.write(buffer,0,n); } //也可以用IOUtils.copy(inputStream,os); MultipartFile multipartFile = new CommonsMultipartFile(fileItem); System.out.println(multipartFile.getName()); return multipartFile; }catch (IOException e){ e.printStackTrace(); } return null; } (3):使用FileUtils.copyInputStreamToFile() 也是会生成文件,到最后也是要删除文件 public static void main(String[] args) throws Exception { String path = "D:\\demo\\"; File file = new File(path,"demo.txt"); // 得到MultipartFile文件 MultipartFile multipartFile = getFile(); // 把流输出到文件 FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file); // 读取文件第一行 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); System.out.println(bufferedReader.readLine()); // 输出绝对路径 System.out.println(file.getAbsolutePath()); bufferedReader.close(); // 操作完上的文件 需要删除在根目录下生成的文件 if (file.delete()){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } } /** * * @Description 返回MultipartFile文件 * @return org.springframework.web.multipart.MultipartFile */ public static MultipartFile getFile() throws IOException { String filePath = "D:\\test.txt"; File file = new File(filePath); FileItem fileItem = new DiskFileItem("copyfile.txt", Files.probeContentType(file.toPath()),false,file.getName(),(int)file.length(),file.getParentFile()); byte[] buffer = new byte[4096]; int n; try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){ while ( (n = inputStream.read(buffer,0,4096)) != -1){ os.write(buffer,0,n); } //也可以用IOUtils.copy(inputStream,os); MultipartFile multipartFile = new CommonsMultipartFile(fileItem); System.out.println(multipartFile.getName()); return multipartFile; }catch (IOException e){ e.printStackTrace(); } return null; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。