Uploaded File, InputStream

900 views
Skip to first unread message

Nanang Suryadi

unread,
Jan 20, 2014, 9:49:20 PM1/20/14
to acti...@googlegroups.com
please help me igor,
i cannot fill my upload file in server ... always got 0kb 
                 
                    fileName = item.getFileName();
                    fileContent = Util.read(item.getInputStream());
                    // logInfo(fileContent);
                    inputStream = item.getInputStream();
                    // write the inputStream to a FileOutputStream
                    outputStream = new FileOutputStream(new File(getRealPath("") + "/out"));

                    int read;
                    byte[] bytes = new byte[1024];

                    while ((read = inputStream.read(bytes)) != -1) {
                        outputStream.write(bytes, 0, read);
                    }

                    System.out.println("Done!");

Igor Polevoy

unread,
Jan 20, 2014, 11:57:52 PM1/20/14
to acti...@googlegroups.com
Nanang, you can look at this example: 

you can also pull the Kitchensink project, run it locally and see how it works. 

tx

Nanang Suryadi

unread,
Jan 21, 2014, 1:43:33 AM1/21/14
to acti...@googlegroups.com
i had been trying,
fileContent has filled , but it cannot write in the file

Why always got 0kb

Thanks


--
--
You received this message because you are subscribed to the Google
Groups "ActiveWeb Group" group.
To post to this group, send email to acti...@googlegroups.com
To unsubscribe from this group, send email to
activeweb+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/activeweb?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "ActiveWeb Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to activeweb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Nanang Suryadi

Igor Polevoy

unread,
Jan 21, 2014, 1:50:55 AM1/21/14
to acti...@googlegroups.com
Nanang, you can ONLY read any InputStream once. This is a stream, remember. 
Once you execute this:
fileContent = Util.read(item.getInputStream());

use fileContent, not stream 

tx

Nanang Suryadi

unread,
Jan 21, 2014, 3:32:11 AM1/21/14
to acti...@googlegroups.com
thanks igor,
it worked ... ,i forget it that stream was :-)

Gianna Giavelli

unread,
Apr 2, 2015, 3:58:48 AM4/2/15
to acti...@googlegroups.com
My solution.

 
This uploads the file into a uploads directory then generates a large file size limited to 900x900 at fullsize directory and limited to 120x120 at thumbnail directory.

I still have to add generating a unique guid and time based filed name. 

  Hope this saves people time 
 

  Gia 

 
--------------
package app.controllers;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import org.javalite.activeweb.AppController;
import org.javalite.activeweb.FormItem;

import java.io.BufferedInputStream; 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;

/**
 * @author Gianna Giavelli
 */
public class UploadController extends AppController 

   public void index() {}

   @POST
   public void save() throws IOException 
   {
       List<FormItem> items = multipartFormItems();
       List<String> messages = new ArrayList<String>();

       for (FormItem item : items) 
       {
           if (item.isFile())
           {
                messages.add ("Found file: " + item.getFileName() );


                //lotsa hoops to find path   is there easier way?
            URL r = this.getClass().getResource("/");
            String strFileName = item.getFileName();
           
            // path decoded "/C:/Program Files/Tomcat 6.0/webapps/myapp/WEB-INF/classes/"
            String decoded = URLDecoder.decode(r.getFile(), "UTF-8");
            String strThumbnailPath = new String();
            String strFullsizePath = new String();

            if (decoded.startsWith("/")) 
                        {
                decoded = decoded.replaceFirst("/", "");
               decoded = decoded.replaceFirst("WEB-INF/classes/", "");
               strThumbnailPath = decoded;
               strFullsizePath = decoded;
               decoded =  decoded.concat("images/upload/");
               decoded =  decoded.concat(strFileName);
               strThumbnailPath = strThumbnailPath.concat("images/thumbnails/");
               strFullsizePath = strFullsizePath.concat("images/fullscreen/");
            }
           
            System.out.println("decoded:" + decoded); //where to go hunt your thumbnails and uploads!
               
               BufferedInputStream oInputStream = new BufferedInputStream (item.getInputStream());
                
               // write the inputStream to a FileOutputStream
                       OutputStream oOutputStream = new FileOutputStream (decoded);
                       int read;
                       byte[] bytes = new byte[1024];

                    while ((read = oInputStream.read(bytes)) != -1) 
                    {
                    // debug    System.out.print ("w");
                        oOutputStream.write(bytes, 0, read);
                    }

                        oOutputStream.close();
               
               //Now use thumbnailinator to create fixed size maxes for the fullscreen and thumbnails version
               File destinationDir = new File(strThumbnailPath);

               Thumbnails.of(decoded)
                       .size(140, 140)
                       .toFiles(destinationDir, Rename.NO_CHANGE); 
            
               File destinationDir2 = new File(strFullsizePath);

               Thumbnails.of(decoded)
                       .size(900, 900)
                       .toFiles(destinationDir2, Rename.NO_CHANGE); 
               
           } //end if file
           else
           {
               messages.add("Found field: " + item.getFieldName() + " with value: " + item.getStreamAsString());
           }
       }// end for item
       
       flash("messages", messages);
       redirect(UploadController.class);
       
   }//end public void save()
}//end class

Igor Polevoy

unread,
Apr 2, 2015, 11:13:17 AM4/2/15
to acti...@googlegroups.com
Looks like you are saving a file into a directory that is under your exploded war file? 

Gianna Giavelli

unread,
Apr 11, 2015, 5:39:08 AM4/11/15
to acti...@googlegroups.com
Yah, I'm trying to find a way to get the active directory where I'm deployed so I don't have to configure the code. That's one way. Since this path will change I wasn't sure if there was a good way to find a relative path for where to put the uploaded file other than this way.

Gianna Giavelli

unread,
Apr 12, 2015, 3:26:01 AM4/12/15
to acti...@googlegroups.com
Is there an easier way? I see the issue as when I'm running under eclipse, I don't have a production environment and so it gets a bit wacky where I can write the file. Of course with a full normal tomcat server, its easy to have a set place to save the upload.  


On Thursday, April 2, 2015 at 10:13:17 AM UTC-5, Igor Polevoy wrote:

Gianna Giavelli

unread,
Apr 13, 2015, 12:42:14 AM4/13/15
to acti...@googlegroups.com
Just wanted to add, one issue would be if you REMOVED or ULOADED your app from the server, which would delete all your uploads, so you can run a batch and copy them off daily for safely. 

I looked at some tomcat things like specifying an upload directory but it wasn't so clear to me and tricky. something to get back to. there is always the issue of the java sandbox and writing outside of your app space. I think the write a chron job to do a daily backup of your uploads is simpler. Typically I never unload my app anyways, only re-post it to tomcat. 

Igor Polevoy

unread,
Apr 14, 2015, 12:14:06 AM4/14/15
to acti...@googlegroups.com
Gianna, most of our projects have some sort of download. For example: https://expresspigeon.com/ has downloads in hundreds of gigabytes. 
Usually we configure a directory: 

/opt/project/name/uploads/123/567

Where 123 is a number generated from user id, and 567 is user ID. This way, we are spreading files across many directories (so as not to have too many files in the same directory). File names are generated GUIDs, 
and their original names are recorded in the database. 

All boxes, including development computers do have corresponding directories. 
Having uploaded files in the directory if your deployment is really not the best idea. 

When we want different configurations per environment, we are using this: https://github.com/javalite/activejdbc/tree/master/app-config throughout all projects to configure properties specific  to different environments. 

tx

Gianna Giavelli

unread,
Apr 30, 2015, 12:02:32 AM4/30/15
to acti...@googlegroups.com
Ahh ok. Yes I forgot about the many directories issue. I had some config setup issue with my tomcat loading the files from a place outside so I was just using file saved to place I knew it would allow access. I was using a base64 with the filename + timestamp so its essentially a guid but with the advantage that its recoverable if you ever wonder what the heck it was to start with.

Igor Polevoy

unread,
Apr 30, 2015, 1:30:03 AM4/30/15
to acti...@googlegroups.com
not a bad idea!
Reply all
Reply to author
Forward
0 new messages