spring 将FIle 转换成MultipartFile
Posted sfchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 将FIle 转换成MultipartFile相关的知识,希望对你有一定的参考价值。
public static void main(String[] args) {
File file = new File("C:\Users\17146\Desktop\a.xlsx");
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "textField";
/**
源码注释
* Create a new {@link FileItem} instance from the supplied parameters and
* any local factory configuration.
*
* @param fieldName The name of the form field.
* @param contentType The content type of the form field.
* @param isFormField <code>true</code> if this is a plain form field;
* <code>false</code> otherwise.
* @param fileName The name of the uploaded file, if any, as supplied
* by the browser or other client.
*
* @return The newly created file item.
*/
FileItem item = factory.createItem(textFieldName, "text/plain", true, "C:\Users\17146\Desktop\a.xlsx");
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream fis = new FileInputStream(file);
OutputStream os = item.getOutputStream();
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
MultipartFile multipartFile = new CommonsMultipartFile(item);
//这是封装pio 将 excel 读取成二位字符串数组 只做类型转换请忽略下面代码
List<String[]> list = readGameExcel(multipartFile);
for (String[] strings:list ) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < strings.length; i++) {
sb.append(Strings.nullToEmpty(strings[i]));
}
if(Strings.isNullOrEmpty(sb.toString().replaceAll(" ",""))){
continue;
}else {
for (String s : strings) {
System.out.print(s + "---");
}
}
System.out.println();
}
}
以上是关于spring 将FIle 转换成MultipartFile的主要内容,如果未能解决你的问题,请参考以下文章
关于 spring.http.multipart.max-file-size 与 spring.servlet.multipart.max-file-size 的混淆
无法在 Spring Boot 2 中设置“spring.servlet.multipart.max-file-size”的最大值
当 spring.http.multipart.enabled=false 并使用 Apache Commons File Upload 时出现“Servlet 没有多部分配置”500 错误