How can I get the user to upload a file? (Java, MVC, Excel, POI)

I use Struts2, Spring and Hibernate.

I wrote a simple Excel file called test.xls with a POI. When I run the action, it works because test.xls appears, but in my tomcat folder.

I want this file to be able to be downloaded on my jsp page. How do I get a user to upload a .xls file? Rather, what do I call this path?

Sorry if the answer is obvious, I'm new to this. I know this probably has something to do with ServletContext or HttpServletRequest?

[edit] I got his job thanks to the answers below. Here is the working code:

private InputStream excelStream; public String export(){ //Create excelfile HSSFWorkbook workbook = new HSSFWorkbook(); FileOutputStream file = null; try{ file = new FileOutputStream("poi-test.xls"); } catch(IOException e){ e.printStackTrace(); } //Populate workbook with all the excel stuff ... //Write to file ByteArrayOutputStream baos = new ByteArrayOutputStream(); try{ workbook.write(file); workbook.write(baos); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); excelStream = bis; } catch(IOException e){ e.printStackTrace(); } return "excel"; } public InputStream getExcelStream() { return excelStream; } public void setExcelStream(InputStream excelStream) { this.excelStream = excelStream; } 

In my struts file:

  <result name="excel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">excelStream</param> <param name="contentDisposition">attachment; filename="excelExport.xls"</param> <param name="bufferSize">1024</param> </result> 
+1
source share
2 answers

For this you want to use the Struts2 Stream Result. Here is some information about this:

+1
source

If the newly created Excel file is accessible from a web server, why not just point this HTTP link to your Excel file on the JSP page? If you do not want to transfer the excel file dynamically, and not physically create it, as it is currently, this is probably the easiest solution.

+1
source

Source: https://habr.com/ru/post/1396617/


All Articles