我正在做一个服务项目,他的UI允许用户上传文件。我需要写一个服务,可以上传这个文件到服务器,并读取和显示此文件的内容。有人能告诉我怎么做吗?
//Controller definition begins
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody
String uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath()
+ File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
logger.info("Server File Location="
+ serverFile.getAbsolutePath());
return "You successfully uploaded file=" + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name
+ " because the file was empty.";
}
}现在我想知道如何显示上传文件的内容。我需要将它转换回xlsx(上传的文件是xlsx),或者能够直接从它读取数据来更新我正在使用apache commons-io的Db.Also和Spring MVC中的文件上传,正如您从上面的代码中看到的那样。
发布于 2016-08-29 16:00:11
您需要一个能够读取xlsx文件类型的库,比如Apache POI
如果您选择此库,则有一个very good example section in the example secion of it's website
https://stackoverflow.com/questions/39189176
复制相似问题