Java 各种文件类型转换的方法

x33g5p2x  于2022-03-04 转载在 Java  
字(3.3k)|赞(0)|评价(0)|浏览(464)

1.网络资源转File

2.网络资源转MultipartFile

3.File转MultipartFile

4.File转字节数组

5.Frame转BufferedImage

6.BufferedImage转MultipartFile

1.网络资源转File

需要引入依赖commons-io

  1. /**
  2. * 读取网络中的图片
  3. * @param url https://www.kziyue.com/wp-content/uploads/2019/06/5bca-hxyuaph9825616.jpg
  4. * @return
  5. */
  6. public File URLToFile(String url){
  7. File file1 = new File("test.png");
  8. try {
  9. URL url1 = new URL(url);
  10. FileUtils.copyURLToFile(url1,file1);
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. File absoluteFile = file1.getAbsoluteFile();
  15. return file1;
  16. }

2.网络资源转MultipartFile

需要引入依赖spring-web

  1. /**
  2. * 文件上传
  3. * @param urlStr url地址
  4. * @return multipartFile
  5. */
  6. public MultipartFile fileUpload(String urlStr) throws Exception {
  7. try {
  8. //把地址转换成URL对象
  9. URL url = new URL(urlStr);
  10. //创建http链接
  11. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  12. //设置超时间为3秒
  13. conn.setConnectTimeout(3*1000);
  14. //防止屏蔽程序抓取而返回403错误
  15. conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");
  16. //得到输入流
  17. InputStream inputStream = conn.getInputStream();
  18. //截取链接中的文件名
  19. String fileName= urlStr.substring(urlStr.lastIndexOf("/")+1);
  20. MultipartFile multipartFile = new MockMultipartFile(fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
  21. return multipartFile;
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. throw new Exception();
  26. }

3.File转MultipartFile

需要引用的依赖spring-text,httpcore

  1. /**
  2. * 文件类型转换
  3. *
  4. * @param filePath 文件file
  5. * @return MultipartFile
  6. */
  7. public static MultipartFile caseFileToMultipartFile(File filePath) {
  8. MultipartFile multipartFile = null;
  9. try {
  10. log.info("开始进行文件转换");
  11. FileInputStream fileInputStream = new FileInputStream(filePath);
  12. multipartFile = new MockMultipartFile(filePath.getName(), filePath.getName(),
  13. ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. return null;
  17. }
  18. return multipartFile;
  19. }

4.File转字节数组

  1. /**
  2. * 将文件转为字节数组
  3. * @param file
  4. * @param size 1024
  5. * @return
  6. */
  7. public static byte[] BufferStreamForByte(File file, int size) {
  8. byte[] content = null;
  9. try {
  10. BufferedInputStream bis = null;
  11. ByteArrayOutputStream out = null;
  12. try {
  13. FileInputStream input = new FileInputStream(file);
  14. bis = new BufferedInputStream(input, size);
  15. byte[] bytes = new byte[1024];
  16. int len;
  17. out = new ByteArrayOutputStream();
  18. while ((len = bis.read(bytes)) > 0) {
  19. out.write(bytes, 0, len);
  20. }
  21. bis.close();
  22. content = out.toByteArray();
  23. } finally {
  24. if (bis != null) {
  25. bis.close();
  26. }
  27. if (out != null) {
  28. out.close();
  29. }
  30. }
  31. } catch (IOException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. return content;
  36. }

5.Frame转BufferedImage

需要引入依赖javacv

  1. public static BufferedImage FrameToBufferedImage(Frame frame) {
  2. //创建BufferedImage对象
  3. Java2DFrameConverter converter = new Java2DFrameConverter();
  4. BufferedImage bufferedImage = converter.getBufferedImage(frame);
  5. return bufferedImage;
  6. }

6.BufferedImage转MultipartFile

  1. public static MultipartFile fileCase(BufferedImage image){
  2. //得到BufferedImage对象
  3. // BufferedImage bufferedImage = JoinTwoImage.testEncode(200, 200, url);
  4. MultipartFile multipartFile= null;
  5. try {
  6. //创建一个ByteArrayOutputStream
  7. ByteArrayOutputStream os = new ByteArrayOutputStream();
  8. //把BufferedImage写入ByteArrayOutputStream
  9. ImageIO.write(image, "jpg", os);
  10. //ByteArrayOutputStream转成InputStream
  11. InputStream input = new ByteArrayInputStream(os.toByteArray());
  12. //InputStream转成MultipartFile
  13. multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. return multipartFile;
  18. }

如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。 

相关文章

最新文章

更多