码迷,mamicode.com
首页 > 编程语言 > 详细

spring 将FIle 转换成MultipartFile

时间:2020-05-12 13:41:33      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:commons   rgs   text   inpu   any   isnull   bytes   xlsx   com   

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

标签:commons   rgs   text   inpu   any   isnull   bytes   xlsx   com   

原文地址:https://www.cnblogs.com/sfchen/p/12875407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!