远程文件转MultipartFile,并获取ContentType

远程文件转MultipartFile,并获取ContentType

工作中一场景,需要把fastdfs服务器上的远程文件转化成MultipartFile,用来上传到minio服务器上。

遇到一个问题,需要动态的获取到文凭的ContentType,以确保文件或图片能正常在浏览器中预览或下载(图片预览,其他文件直接下载),因为如果ContentType类型设置不正确会导致文件不能正常预览或下载。

代码如下:

1、远程文件Url转为MultipartFile

// 远程文件Url转为MultipartFile
public static MultipartFile uploadImgUrlToMultipartFile(String url) {
  String fileName = FilenameUtils.getName(url);
  // 文件类型
  String contentType = MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
  byte[] bytes = downloadPicture(url);
  return getMultipartFile(fileName, bytes, contentType);
}

2、远程文件Url转化为二进制

// 远程文件Urll转化为二进制
private static byte[] downloadPicture(String url) {
  URL urlConnection;
  HttpURLConnection httpURLConnection = null;
  try {
    urlConnection = new URL(url);
    httpURLConnection = (HttpURLConnection) urlConnection.openConnection();
    InputStream in = httpURLConnection.getInputStream();
    byte[] buffer = new byte[1024];
    int len;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((len = in.read(buffer)) != -1) {
      out.write(buffer, 0, len);
    }
    in.close();
    out.close();
    return out.toByteArray();
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    assert httpURLConnection != null;
    httpURLConnection.disconnect();
  }
  return null;
}

3、二进制文件转换MultipartFile

// 二进制文件转换MultipartFile
public static MultipartFile getMultipartFile(String name, byte[] bytes, String contentType) {
  MultipartFile multipartFile;
  ByteArrayInputStream in;
  try {
    in = new ByteArrayInputStream(bytes);
    FileItemFactory factory = new DiskFileItemFactory(16, null);
    FileItem fileItem = factory.createItem("attachFile", contentType, false, name);
    IOUtils.copy(new ByteArrayInputStream(bytes), fileItem.getOutputStream());
    multipartFile = new CommonsMultipartFile(fileItem);
    in.close();
  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
  return multipartFile;
}

知识点:

1、FilenameUtils工具类:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

常用方法:

 log.info("fullPath:{}", FilenameUtils.getFullPath(url));
 log.info("getFullPathNoEndSeparator:{}", FilenameUtils.getFullPathNoEndSeparator(url));
 log.info("getPathNoEndSeparator:{}", FilenameUtils.getPathNoEndSeparator(url));
 log.info("getPath:{}", FilenameUtils.getPath(url));
 log.info("getPrefixLength:{}", FilenameUtils.getPrefixLength(url));
 log.info("getPrefix:{}", FilenameUtils.getPrefix(url));
 log.info("getName:{}", FilenameUtils.getName(url));
 log.info("getBaseName:{}", FilenameUtils.getBaseName(url));
 log.info("getExtension:{}", FilenameUtils.getExtension(url));

- fullPath:http://192.168.36.157:8888/group1/M00/0D/ED/
- getFullPathNoEndSeparator:http://192.168.36.157:8888/group1/M00/0D/ED
- getPathNoEndSeparator:http://192.168.36.157:8888/group1/M00/0D/ED
- getPath:http://192.168.36.157:8888/group1/M00/0D/ED/
- getPrefixLength:0
- getPrefix:
- getName:wAYknWHX56yAUWb_AAGlwdIDVOY330.jpg
- getBaseName:wAYknWHX56yAUWb_AAGlwdIDVOY330
- getExtension:jpg

2、ContentType类型:

如下代码可根据文件名后缀转换成对应的ContentType类型,如果找不到的其他类型转换成 application/octet-stream 类型,以便可以直接下载文件使用。

//获取远程文件url 的 contentType 类型
String contentType = MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
参数说明
text/plain纯文本格式
text/htmlHTML格式
text/xmlXML格式
image/gifgif图片格式
image/jpegjpg图片格式
image/pngpng图片格式
application/xhtml+xmlXHTML格式
application/xmlXML数据格式
application/atom+xmlAtom XML聚合格式
application/jsonJSON数据格式
application/pdfpdf格式
application/mswordWord文档格式
application/octet-stream二进制流数据(如常见的文件下载)
application/x-www-form-urlencodedform表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
multipart/form-data在表单中进行文件上传时






  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值