MultipartFile与File的一些事 |
您所在的位置:网站首页 › multipartfile文件名乱码 › MultipartFile与File的一些事 |
前言
前段时间碰到了中转文件的需求,需要使用HttpClient中转一下文件,过程为: MultipartFile是spring类型,代表HTML中form data方式上传的文件,包含二进制数据+文件名称。【来自百度知道】 MultipartFile 与 File 的 互相转换 1. File转MultipartFile(1):使用org.springframework.mock.web.MockMultipartFile 需要导入spring-test.jar public static void main(String[] args) throws Exception { String filePath = "F:\\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 }(2):使用CommonsMultipartFile public static void main(String[] args) throws Exception { String filePath = "F:\\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(); } } 2. MultipartFile转File(1):使用File转MultipartFile的逆过程 你在看这个代码的时候会觉得很熟悉,是的这个就是File转MultipartFile的逆转过程,这个方法会在根目录生成一个文件,需要删除该文件。 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 * @date 2019/1/5 11:08 * @auther dell */ public static MultipartFile getFile() throws IOException { String filePath = "F:\\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; }(2):使用transferTo (本质上还是使用了流 只不过是封装了步骤) 会生成文件,最后不需要文件要删除 public static void main(String[] args) throws Exception { String path = "F:\\demo\\"; 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 * @date 2019/1/5 11:08 * @auther dell */ public static MultipartFile getFile() throws IOException { String filePath = "F:\\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 = "F:\\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 * @date 2019/1/5 11:08 * @auther dell */ public static MultipartFile getFile() throws IOException { String filePath = "F:\\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; }4:强转类型 CommonsMultipartFile commonsmultipartfile = (CommonsMultipartFile) multipartFile; DiskFileItem diskFileItem = (DiskFileItem) commonsmultipartfile.getFileItem(); File file = diskFileItem.getStoreLocation();这种强转你获得的file只是一个空壳 这里不自己给例子了,参考了其他博客的代码 // 获取文件名称 String fileName = file.getOriginalFilename(); HttpPost httpPost = new HttpPost(url); // 创建文件上传实体 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName); builder.addTextBody("filename", fileName); HttpEntity entity = builder.build(); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost);// 执行提交执行提交之后你会发现你上传的文件名会出现中文乱码 这里参考 HttpClient上传文件中文名乱码 该博客详细说明了为什么会乱码以及怎么解决 我使用的解决办法是: //设置模式为RFC6532 builder.setMode(HttpMultipartMode.RFC6532); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |