当前位置:网站首页>Multipartfile and file interoperation tool classes
Multipartfile and file interoperation tool classes
2022-06-11 01:36:00 【Vault of heaven】
rely on :
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>package com.ciih.workshop.utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* MultipartFile and File Mutual conversion tools
*/
public class MultipartFileUtil {
/**
* Input flow MultipartFile
*
* @param fileName
* @param inputStream
* @return
*/
public static MultipartFile getMultipartFile(String fileName, InputStream inputStream) {
MultipartFile multipartFile = null;
try {
multipartFile = new MockMultipartFile(fileName, fileName,
ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
} catch (Exception e) {
e.printStackTrace();
}
return multipartFile;
}
/**
* Read network files
*
* @param url File address
* @param fileName File name ( File name suffix required )
* @return
*/
public static MultipartFile getMultipartFile(String url, String fileName) {
HttpResponse response = HttpRequest.get("http://atcc-workshop.ciih.net/M00/00/45/wKgBNWKQJtSAMYcyAAAWABukO1g730.xls").execute();
InputStream inputStream = response.bodyStream();
return MultipartFileUtil.getMultipartFile(fileName, inputStream);
}
/**
* File turn MultipartFile
*
* @param file
* @return
*/
public static MultipartFile getMultipartFile(File file) {
FileInputStream fileInputStream = null;
MultipartFile multipartFile = null;
try {
fileInputStream = new FileInputStream(file);
multipartFile = new MockMultipartFile(file.getName(), file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
} catch (Exception e) {
e.printStackTrace();
}
return multipartFile;
}
/**
* MultipartFileUtil turn File
*
* @param multipartFile
* @return
*/
public static File getFile(MultipartFile multipartFile) {
String fileName = multipartFile.getOriginalFilename();
File file = new File(fileName);
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] ss = multipartFile.getBytes();
for (byte s : ss) {
out.write(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// File f = new File(file.toURI());
// if (f.delete()) {
// System.out.println(" Delete successful ");
// } else {
// System.out.println(" Delete failed ");
// }
return file;
}
}
边栏推荐
- IRS应用发布之十五:应用安全自测指南
- 北京門頭溝區高新技術企業培育支持標准,補貼10萬
- Understanding of multithreading
- SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
- Introduction to the application process of China Patent Award, with a subsidy of 1million yuan
- Introduction to prefix, infix and suffix expressions (code implementation of inverse Polish calculator)
- Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
- Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
- [ROS] review of 2021 ROS Summer School
- How to use user-defined annotation for parameter verification
猜你喜欢

云呐|PDA无线固定资产盘点管理系统

对象存储 S3 在分布式文件系统中的应用

Direct insert sort and shell sort

What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture

A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
![[VBA Script] extract the information and pending status of all annotations in the word document](/img/dc/0db51d092cde019cef4113796e4882.png)
[VBA Script] extract the information and pending status of all annotations in the word document

Once you know these treasure websites, you can't live without them!!!
![[ROS] review of 2021 ROS Summer School](/img/1c/588d29b60071628c7c9fdce17e8b84.jpg)
[ROS] review of 2021 ROS Summer School

多兴趣召回模型实践|得物技术
随机推荐
detectron2训练自己的数据集和转coco格式
北京企业前沿引领技术创新补助资金介绍,补助500万
复利的保险理财产品怎么样?可以买吗?
简述自定义注解
Beijing Pinggu District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
1.4PX4程序下载
[VBA Script] extract the information and pending status of all annotations in the word document
2022 recognition requirements for new technologies and new products (services) in Huairou District, Beijing
1.2、ROS+PX4预备基础知识
中间件_Redis_06_Redis的事务
Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system
Introduction to the subsidy fund for leading technological innovation of Beijing enterprises, with a subsidy of 5million yuan
【ROS】ROSmsg cakin_ Make compilation error
关于mobx
数字ic设计自学ing
QGC地面站使用教程
Hooks' design philosophy
Introduction and creation of Huffman tree
[path planning] week 1: Path Planning open source code summary (ROS) version
[ROS introduction] cmakelist Txt and packages XML interpretation