首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache-commons-fileupload:如何读取和显示spring mvc上传到服务器目录的临时文件中的数据

Apache-commons-fileupload:如何读取和显示spring mvc上传到服务器目录的临时文件中的数据
EN

Stack Overflow用户
提问于 2016-08-28 16:02:24
回答 1查看 336关注 0票数 0

我正在做一个服务项目,他的UI允许用户上传文件。我需要写一个服务,可以上传这个文件到服务器,并读取和显示此文件的内容。有人能告诉我怎么做吗?

代码语言:javascript
复制
//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中的文件上传,正如您从上面的代码中看到的那样。

EN

回答 1

Stack Overflow用户

发布于 2016-08-29 16:00:11

您需要一个能够读取xlsx文件类型的库,比如Apache POI

如果您选择此库,则有一个very good example section in the example secion of it's website

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39189176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档