开发者

Reading the content of the uploaded file in Struts2

开发者 https://www.devze.com 2023-03-13 05:06 出处:网络
How can I read the content of an uploaded file in the 开发者_如何学运维execute() method of the Action class? I am able to upload the file on the server but do not know how to read the content of that

How can I read the content of an uploaded file in the 开发者_如何学运维execute() method of the Action class? I am able to upload the file on the server but do not know how to read the content of that file.

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


Option 1: Create a servlet

I recommend you utilize apache commons file upload. This link has examples on how to process the uploaded file (writing it to a disk or reading it in memory if the file size is small enough) using FileItem. Another relevant example can be found here.

Option 2: using the struts s:file tag

As @BalusC mentioned in the comments below struts has a built in file upload process using s:file tag library and a tutorial of using it is provided here. Essentially the file gets uploaded to a temporary directory. However, you can override that by setting a value for the struts.multipart.saveDir property in the default.properties file. This link also mentions using Apache FileUtils to process the uploaded file afterwards which by the way is a very handy library for any File I/O work.


http://java.dzone.com/articles/struts2-tutorial-part-67 Here he explains how to make a very clear and detailed upload


Here's the standard way that provides Struts2, with an example:

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

It's quite simple and elegant (no need to mess with servletRequest.getRealPath("/") as ther other example linked by hoss).

By using the <s:file> tag (and the appropiate interceptor), Struts2 makes all the dirty work and gives you the (temporary) uploaded file as a File field in the action; you can open it or move, or whatever, as you do with any file.

0

精彩评论

暂无评论...
验证码 换一张
取 消