jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Displaying file content in a jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Displaying file content in a jsp page" Watch "Displaying file content in a jsp page" New topic
Author

Displaying file content in a jsp page

rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
How to display a file's content that has been browsed in page in its next page
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
You might be interested in this article

- Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
sorry, don't get the question. can you rephrase it or post a little example of you problem?

what do you mean by "browse a file"?

regards,
jan
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
i am uploading a file from my web page.i have to display the contents of the file in the next jsp page.
My jsp page is
<html:form action="uploadfile.do" method="post" enctype="multipart/form-data">

<table align="center">
<tr>
<td align="center" colspan="2">
<font size="4">Please Enter the Following Details</font>
</tr>

<tr>
<td align="left" colspan="2">
<font color="red"><html:errors/></font>
</tr>



<tr>
<td align="right">
File Name
</td>
<td align="left">
<html:file property="theFile" name="uploadFileForm"/>
</td>
</tr>


<tr>
<td align="center" colspan="2">
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>


</html:form>
My action file
public class UploadFileAction extends Action{
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("entering in to action");
UploadFileForm myForm = (UploadFileForm)form;

// Process the FormFile
FormFile myFile = myForm.getTheFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
System.out.println("contentType: " + contentType);
System.out.println("File Name: " + fileName);
System.out.println("File Size: " + fileSize);
System.out.println("FileContent"+fileData);
request.getSession().setAttribute("myFile",myFile );
return mapping.findForward("success");
}

}

My action form is
public class UploadFileForm extends ActionForm{

private FormFile theFile;

/**
* @return Returns the theFile.
*/
public FormFile getTheFile() {
return theFile;
}
/**
* @param theFile The FormFile to set.
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}


}
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
hm, not so easy to read if you don't use the code tag... :-(

anyhow, displaying a file's content strongly depends on the type of the file. are you able to make some assumptions on this?

if it's something text based, i'd simply parse the file into the underlaying form (server-side, of course) and display the appropriate contents / properties on the jsp. depends on you whether you want the whole text as a chunk of data or write getters which read it out line-wise.

please feel free to ask further questions,

jan
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
Hi i would like to display the text file as a chunk of data.
Can you please tell me how to display this in next jsp page
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
well, if you uploaded the file, don't put the file itself in the form, but it's content in a readable way. (maybe String is enough...)

write getters that return the content the way you need it.

use them in from the jsp.

should uploading be your problem, take this as an example.

:-)

cheers,
jan
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
oops, just realized that i posted the same link as in #1 - guess this is what google returns when typing in "struts file upload" - did you take look at it?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The upload example in the struts-examples.war file that comes with the Struts download does exactly what you are trying to do. Study thier code, and you will see how it's done.


Merrill
Consultant, Sima Solutions
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
Thank you am trying with the same example.
Am getting the first page displayed.
but when i click on the submit
am getting the following error
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
common-fileupload jar file comes with struts example. Place this jar in lib folder of your application and then start server.

Naseem
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
Hi
I am facing problem in uploading files other than explorer files.


If i try to upload a html file its getting opened in a separate jsp page but not in my success jsp page.
Am getting an error
Cannot forward after response has been committed
Can anyone plesae help me out in this issue
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Because your Action class is writing an HTML response, you cannot forward to a JSP afterward. You must return null from your perform() method just as the example in struts-examples.war does.

There can only be one response, and if you generate it using the HttpServletResponse object's OutputStream object, you cannot then forward to a JSP to generate the response. You must choose one or the other. You can't do both.

Note: Unless you're using Struts 1.0, I'd recommend you change the name of your method from perform to execute. The perform method is deprecated, and if you ever migrate to a later version, your applicatoin will stop working.
[ August 30, 2006: Message edited by: Merrill Higginson ]
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
While trying with struts examples-upload
am getting the html file uploaded in the next page
but am not getting the word and excel files in the same format they are getting uploaded in different format.
Another problem i face is i am not able to upload the file in to another file.
Here is the action code

Am using struts 1.0
So ihave used Perform method
Can anyone please help me out to solve the problem.
rakshini nithya
Ranch Hand

Joined: Jun 15, 2006
Posts: 39
How to make a word document displayed in a jsp page?
Am able to upload the word file in jsp page.
but its getting uploaded in a different format
How to open the word file exactly in the jsp page?
Can anyone please help me out?
Thanks in advance.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
in order to display a word document you will need to parse it.

this can be done by using either apache poi or the open-office api.

both is a considerable amount of work.

i'm not sure if ms internet explorer can do something native, think i've seen web-sites which display excel-docs in the ie.

hope this helps,
jan
[ August 31, 2006: Message edited by: Jan Groth ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying file content in a jsp page
 
Similar Threads
HTML Link to a Quicktime file displays garbage characters in IE
update page content
Content Type problem
case study: where to use Ajax
XMLHTTPRequest Handling HTML Response