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();
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM