繁体   English   中英

如何从临时文件夹中呈现JSP页面中的上载Excel文件?

[英]How to Render uploaded Excel file in JSP Page from a Temp Folder?

我不清楚是否将上传的Excel工作表呈现到JSP页面中

这是上传Excel的示例代码

importExcel.jsp

<body>
<form:form method="POST" action="fileUpload"  enctype="multipart/form-data">
  <div class="upload">
 <div class="upload-files">
  <header>
   <p>
    <i class="fa fa-cloud-upload" aria-hidden="true"></i>
    <span class="up">up</span>
    <span class="load">Load</span>
   </p>
  </header>
  <div class="body" id="drop">
   <i class="fa fa-file-text-o pointer-none" aria-hidden="true"></i>
   <p class="pointer-none"><b>Drag and drop</b> files here <br /> or <a href="" id="triggerFile">browse</a> to begin the upload</p>
            <input type="file" name="xlsFile" accept=".xls,.xlsx" />
  </div>
  <footer>
   <div class="divider">
    <span><AR>FILES</AR></span>
   </div>
   <div class="list-files">
    <!--   template   -->
   </div>
            <button class="importar">UPDATE FILE</button>
  </footer>
 </div>
</div>


</form:form>
</body>

HomeController.java

@PostMapping("/fileUpload")
    public String getFileUploadResult(@RequestParam("xlsFile") MultipartFile multiPartFile )throws Exception{
        try {

            if(multiPartFile!=null && !multiPartFile.isEmpty()){
                byte[] fileBytes = multiPartFile.getBytes();
                if(fileBytes!=null){
                    System.out.println("multiPartFile.getOriginalFilename() :: "  +multiPartFile.getOriginalFilename());
                    Path internalPath=Paths.get(RAW_DATA_FILE_PATH+multiPartFile.getOriginalFilename());
                    if(internalPath!=null){
                        System.out.println("file written");
                        Files.write(internalPath,fileBytes);
                    }else{
                        System.out.println("internalPath is null");
                    }
                }else{
                    System.out.println("fileBytes is null");
                }
            }else{
                return "importExcel";
            }
        } catch (Exception e) {
            throw e;
        }
        return "viewUploadedExcel";
    }

我需要在viewUploadedExcel.jsp文件中查看上载的Excel文件,因为我正在对此进行研究以提出解决方案,但尚未达到期望。

注意 :我正在开发JSP和Spring

只需将响应contentType中的mime / type设置为application / vnd.ms-excel之类的文件即可,具体取决于文件格式。 基于文件扩展名,xlsx将为application / vnd.openxmlformats-officedocument.spreadsheetml.sheet。 然后只需使用out.write上载文件的内容。 显然,浏览器对excel的呈现取决于浏览器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM