• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying file content in a jsp page

 
rakshini nithya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to display a file's content that has been browsed in page in its next page
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in this article

- Naseem
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rakshini nithya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic