Re: Struts 2 File upload to store the filedata
Even i was trying implementing the same think but i couldnt make it. I used result name=success type=stream image/jpeg inputStream filename=${filename} 1024 /result But got an exception saying Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. 1) couldn't make out what was going wrong. 2)and if i wish to display in my desired .jsp page with single/multiple images in it how do i achieve it? 3)and can i use result type as a tiles 2.0? -- View this message in context: http://old.nabble.com/Struts-2-File-upload-to-store-the-filedata-tp14168069p30776306.html Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 File upload to store the filedata
Jeromy Evans - Blue Sky Minds wrote: Hi Johnson, I think just need to read up a little more on Struts 2 as it seems you're trying to do too much yourself based on Struts1 experiences rather than let the framework do more of the routine work. Ian Roughly's book is a good place to start: http://www.infoq.com/minibooks/starting-struts2 Anyway, in a previous email you mentioned you know how to open an input stream from the image data in your database: InputStream in = rs.getBinaryStream(filedata); The StreamResult requires that an input stream is open and available in a public property in your action. Here's a section from your new action: public class MyAction extends ActionSupport { // private InputStream imageStream; public String execute() { // read data from the database ... //... imageStream = rs.getBinaryStream(filedata); // ... return SUCCESS; } /** Return an InputStream for the StreamResult */ public InputStream getImageStream() { return imageStream; } That's it. We have an open InputStream in a public property in your action. You don't do anything other than prepare it for reading. Now your struts.xml requires the action and result definition. The definition below states that when your action returns success, the stream result type (StreamResult) should be used and that the name of the InputStream property is imageStream (ie. getImageStream()). That ties it back into the code above. action name = myaction class=MyAction result name=success type = stream image/jpeg imageStream filename=image.jpg 1024 /result /action Hope that helps. After you have that working, you can also experiment with getting the content type and filename from properties in your action. Hint: ${contentType} Regards, Jeromy Evans - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org Hi, Jeromy, I used the above method to upload and display the images and succeed. However, if I want to show many images on one page, such as a table like this: user_id_1 user_name_1 user_portrait_1(image) user_id_2 user_name_2 user_portrait_2(image) user_id_3 user_name_3 user_portrait_3(image) user_id_4 user_name_4 user_portrait_4(image) user_id_5 user_name_5 user_portrait_5(image) Is there a way to do it? I used to use display tag to solve similar table display, but with image URL, it doesn't work. -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tp14168069p23970105.html Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2 File upload to store the filedata
a static inner class example is located at org.apache.struts2.util.StrutsUtil called static class ResponseWrapper notice the sout = new ServletOutputStreamWrapper(strout); assignment Difficult to determine what your needs are without more information M-- - Original Message - From: Johnson nickel [EMAIL PROTECTED] To: user@struts.apache.org Sent: Friday, December 07, 2007 8:39 AM Subject: Re: Struts 2 File upload to store the filedata Hi Evans, I digged in google examples in StreamResult. But, i can't understand. I want to display the images in browser Using StremResult. Instead of ServletOutputStream out= response.getOutputStream(); How can i use StreamResult. Can u give some examples pgm?? That will be helpful.. Regards, Johnson -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tf4948427.h tml#a14212685 Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Struts 2 File upload to store the filedata
Hi Evans, I digged in google examples in StreamResult. But, i can't understand. I want to display the images in browser Using StremResult. Instead of ServletOutputStream out= response.getOutputStream(); How can i use StreamResult. Can u give some examples pgm?? That will be helpful.. Regards, Johnson -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tf4948427.html#a14212685 Sent from the Struts - User mailing list archive at Nabble.com.
Re: Struts 2 File upload to store the filedata
Don't use ServletOutputStream within Struts2. Use a StreamResult instead: http://struts.apache.org/2.0.11/docs/stream-result.html In the example provided in the above link, inputName references a property in your action that's an InputStream you need to create from your data. InputStream getInputStream(); If you search, you'll find plenty of examples using StreamResult. regards, Jeromy Evans Johnson nickel wrote: Hi Evans, I want to display the images in browser from the database. I have added my code.It throws null pointer Exception. In my action, i have implements ServletRequestAware,ServletResponseAware interface. I want to know if ServletOutputStream is working in Struts 2.0.6 action class. Can u give a solution for this. InputStream in = rs.getBinaryStream(filedata); response.setContentType(image/jpeg); response.addHeader(Content-Disposition,inline;filename= filename); ServletOutputStream out = response.getOutputStream(); out.flush(); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.503 / Virus Database: 269.16.15/1174 - Release Date: 6/12/2007 10:11 AM - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Struts 2 File upload to store the filedata
Hi Evans, I want to display the images in browser from the database. I have added my code.It throws null pointer Exception. In my action, i have implements ServletRequestAware,ServletResponseAware interface. I want to know if ServletOutputStream is working in Struts 2.0.6 action class. Can u give a solution for this. InputStream in = rs.getBinaryStream(filedata); response.setContentType(image/jpeg); response.addHeader(Content-Disposition,inline;filename= filename); ServletOutputStream out = response.getOutputStream(); out.flush(); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tf4948427.html#a14211550 Sent from the Struts - User mailing list archive at Nabble.com.
Re: Struts 2 File upload to store the filedata
Johnson nickel wrote: Hi Evans, I digged in google examples in StreamResult. But, i can't understand. I want to display the images in browser Using StremResult. Instead of ServletOutputStream out= response.getOutputStream(); How can i use StreamResult. Can u give some examples pgm?? That will be helpful.. Regards, Johnson Hi Johnson, I think just need to read up a little more on Struts 2 as it seems you're trying to do too much yourself based on Struts1 experiences rather than let the framework do more of the routine work. Ian Roughly's book is a good place to start: http://www.infoq.com/minibooks/starting-struts2 Anyway, in a previous email you mentioned you know how to open an input stream from the image data in your database: InputStream in = rs.getBinaryStream(filedata); The StreamResult requires that an input stream is open and available in a public property in your action. Here's a section from your new action: public class MyAction extends ActionSupport { // private InputStream imageStream; public String execute() { // read data from the database ... //... imageStream = rs.getBinaryStream(filedata); // ... return SUCCESS; } /** Return an InputStream for the StreamResult */ public InputStream getImageStream() { return imageStream; } That's it. We have an open InputStream in a public property in your action. You don't do anything other than prepare it for reading. Now your struts.xml requires the action and result definition. The definition below states that when your action returns success, the stream result type (StreamResult) should be used and that the name of the InputStream property is imageStream (ie. getImageStream()). That ties it back into the code above. action name = myaction class=MyAction result name=success type = stream param name=contentTypeimage/jpeg/param param name=inputNameimageStream/param param name=contentDispositionfilename=image.jpg/param param name=bufferSize1024/param /result /action Hope that helps. After you have that working, you can also experiment with getting the content type and filename from properties in your action. Hint: param name=contentType${contentType}/param Regards, Jeromy Evans - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Struts 2 File upload to store the filedata
Hi Jeromy Evans, Thanks for your reply. I would like to insert the images into my Databases. For that, i'm using byte[] array. In Struts 1.3, I used FormFile class. From this class i got the method getFileData(); In my db, ps.setBytes(1,filedata); // to store the binary datas in DB. /*FormFile mtfile = form.getTheFile(); byte[] filedata = mtfile.getFileData();*/ In Struts 2.0.6, UploadAction.java private File upload; private String contentType; private String filename; byte [] filedata; // to Store the binary datas public void setUpload(File upload) { this.upload = upload; } same as setUploadContentType,getUploadFileName these are the three methods default for the FileUpload Interceptor. In jsp, s:file=upload label=file/ Regards, John -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tf4948427.html#a14168972 Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Struts 2 File upload to store the filedata
Johnson nickel wrote: Hi Jeromy Evans, Thanks for your reply. I would like to insert the images into my Databases. For that, i'm using byte[] array. In Struts 1.3, I used FormFile class. From this class i got the method getFileData(); In my db, ps.setBytes(1,filedata); // to store the binary datas in DB. /*FormFile mtfile = form.getTheFile(); byte[] filedata = mtfile.getFileData();*/ Ahh, ok. In Struts2, the file is a reference to a temporary file created on your server. If it's not HUGE, just read it into a byte array. The code follows. This code is a fairly standard approach to read an arbitrary length inputstream into a byte array one chunk at a time. If the file can be HUGE, see my comment at bottom. byte[] filedata = readInputStream(new FileInputStream(upload)); /** Read an input stream in its entirety into a byte array */ public static byte[] readInputStream(InputStream inputStream) throws IOException { int bufSize = 1024 * 1024; byte[] content; Listbyte[] parts = new LinkedList(); InputStream in = new BufferedInputStream(inputStream); byte[] readBuffer = new byte[bufSize]; byte[] part = null; int bytesRead = 0; // read everyting into a list of byte arrays while ((bytesRead = in.read(readBuffer, 0, bufSize)) != -1) { part = new byte[bytesRead]; System.arraycopy(readBuffer, 0, part, 0, bytesRead); parts.add(part); } // calculate the total size int totalSize = 0; for (byte[] partBuffer : parts) { totalSize += partBuffer.length; } // allocate the array content = new byte[totalSize]; int offset = 0; for (byte[] partBuffer : parts) { System.arraycopy(partBuffer, 0, content, offset, partBuffer.length); offset += partBuffer.length; } return content; } Disclaimer: This approach is limited to small files (multiple megabytes rather than hundreds/gigs) because of the inefficient memory consumption. It's also quite slow as the commons fileuploader first receives all the data, then writes the entire temporary file, then it's read again entirely into memory, then written back to your database/filesystem. There's no other built-in approach in Struts2 but you'll find ajax fileuploaders on the web that handle massive files better than this. regards, Jeromy Evans - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Struts 2 File upload to store the filedata
Johnson nickel wrote: Hi everyone, I am using Struts 2 and using s:file tag to upload a file. But I get that file as a 'File' object in my action class and using getFileName() on that File object returns me some arbitrary value (something like upload__5b41f107_1127b4befe0__8000_.tmp). I want to retrieve things like Filedata to store the binary datas in my databases. which was possible in struts 1.1 using methods like on getFiledata() using byte [] array FormFile Object. How do I accomplish the same in Struts 2 ? Regards, John Hi John, If the file property in your action is called anUploadFile, then also add the following properties to your action: String anUploadFileContentType String anUploadFileFileName If you require any other kinds of filedata (eg. size) you'll have to obtain it from the File object. ie. void setAnUploadFile(File file); void setAnUploadFileFileName(String name) void setAnUploadFileContentType(String contentType) For more information, see the FileUploadInterceptor: http://struts.apache.org/2.0.11/docs/file-upload-interceptor.html regards, Jeromy Evans - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Struts 2 File upload to store the filedata
Hi everyone, I am using Struts 2 and using s:file tag to upload a file. But I get that file as a 'File' object in my action class and using getFileName() on that File object returns me some arbitrary value (something like upload__5b41f107_1127b4befe0__8000_.tmp). I want to retrieve things like Filedata to store the binary datas in my databases. which was possible in struts 1.1 using methods like on getFiledata() using byte [] array FormFile Object. How do I accomplish the same in Struts 2 ? Regards, John -- View this message in context: http://www.nabble.com/Struts-2-File-upload-to-store-the-filedata-tf4948427.html#a14168069 Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]