wood burning stoves*
The moose likes Struts and the fly likes write the contents that we read from a file to a textbox. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "write the contents that we read from a file to a textbox." Watch "write the contents that we read from a file to a textbox." New topic
Author

write the contents that we read from a file to a textbox.

Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
We want to write the contents that we read from a file to a textbox.

Can anyone please share how to do this.

This is how we are reading the file
public ActionForward doUpload(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception{
ExampleForm csForm = (ExampleForm) form;

// Process the FormFile
FormFile myFile = csForm.getMyFile();
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("File data: " + fileData);

return mapping.findForward("success");
}

User clicks on the Browse button(implemented using html:file tag) to select the fileName and then clicks on Upload button.
This calls the doUpload method that I mentioned above. After this we need to write the contents of the file to a text box shown below the Upload button on the jsp.
Ratheesh Narayanan
Ranch Hand

Joined: Oct 30, 2004
Posts: 38
Add "fileData" to the request as an attribute. Use JSP expression syntax (<%= ... %> to get the data filled in textfield.

OR

You can make a bean with the file information (fileName, fileSize, fileData etc..) and set that bean in request and use syntax like:<jsp:getProperty name="fileBean" property="fileData"/>

Or you can make javascript variable in the action to set the data in the field.

Hope there more better ways. Its pretty simple I guess.

-Ratheesh


SCJP 1.4 & SCBCD 1.5
Vani Bandargal
Ranch Hand

Joined: Oct 06, 2005
Posts: 82
Thanks for the reply. I figured it out that I was not convering Byte to char. Very silly mistake.

Here is what we did

public ActionForward doUpload(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception{
try {
ExampleForm csForm = (ExampleForm ) form;

// Process the FormFile
FormFile myFile = csForm.getMyFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
StringBuffer fileContent = new StringBuffer();

char chars[] = new char[fileData.length];

for (int x = 0; x < fileData.length; x++) {
chars[x] = (char) fileData[x];
fileContent.append(chars[x]);
}




csForm.setUploadFileData(fileContent.toString());

request.setAttribute(mapping.getAttribute(), csForm);

} catch (Exception e) {
System.out.println("Exception: " + e);
logger.error("Exception: "+ e);
throw new WebException(e);
}
return mapping.findForward(IConstants.SUCCESS_KEY);
}

You are correct. as you mentioned we took that to a form variable and the textarea in the jsp is assigned that property.
[ August 01, 2008: Message edited by: Vani D Bandargal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: write the contents that we read from a file to a textbox.