You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Shawn Sohl <SS...@HNTB.com> on 2001/08/07 20:48:46 UTC

Uploading Files

I have looked at multiple sites and through countless articles about how to
upload a file using struts.  I have gotten to the point where I can upload a
file and use the "FormFile" to see things about the file like name and size.
My problem is that I want to store the file on the server in a particular
folder but do not know how to.  I have tried examples using the
.getInputStream and .getFileData methods with no luck.  Basically in my
Action class, I call this function called "test" and pass it the FormFile
object.  
The code in the loop does not even get run.  Any help would be greatly
appreciated....................

private void test(FormFile file) throws Exception
          {

                     //retrieve the content type
                     String contentType = file.getContentType();

                     //retrieve the file data
                     ByteArrayOutputStream baos = new
ByteArrayOutputStream();
                     InputStream stream = file.getInputStream();
                     byte[] buffer = new byte[file.getFileSize()];

                     stream.read(buffer);
                     baos.write(buffer);

                     //write the file to the file specified
                     OutputStream bos = new
FileOutputStream("C:\\test\\book.txt");

                      int bytesRead = 0;

                     while ((bytesRead = stream.read(buffer, 0,
buffer.length)) != -1)
                                  {
                                          bos.write(buffer, 0, bytesRead);
                                  }
 
                     baos.flush();
                     baos.close();
                     bos.close();
         }