Reading the contents of a downloaded file in Struts2

How can I read the contents of a loaded file in a execute()class method Action? I can upload the file to the server, but I don’t know how to read the contents of this file.

Do I need to save it on the server first? Or can we read it directly?

+3
source share
3 answers

Option 1: Creating a Servlet

I recommend that you use apache commons to download the file . This link provides examples of processing a downloaded file (writing it to disk or reading it in memory if the file size is small enough) using FileItem . Another suitable example can be found here .

Option 2: use tags s:file

@BalusC , struts , s: . , . , struts.multipart.saveDir default.properties. Apache FileUtils , , , - .

+4

, Struts2, :

http://struts.apache.org/2.0.14/docs/file-upload.html

It is quite simple and elegant (no need to mess around servletRequest.getRealPath("/")like another hoss related example).

Using the tag <s:file>(and the corresponding interceptor), Struts2 does all the dirty work and gives you the (temporary) downloaded file as a field Filein action; You can open it or move it, or as you like with any file.

+2
source

All Articles