0

My site is designed with JSP pages and Java Servlets.

On my site, I already have the process in place for allowing users to upload files. The files get stored in a folder on my server.

Now I would like to give users the option of uploading a video and store the video on my server, just as I do with the image files.

Questions
1) Can I use my code I already have for uploading files? If so, what do I need to add or change?

2) If I can't use my code, how do I code for the upload of a video?

3) How do I display the video on an Html or JSP page?


Edit - Update

A snippet of the code for uploading a file - and renaming it newvideo.avi

         while(it.hasNext()) {                                 

                FileItem item = (FileItem)it.next(); // Create a FileItem object to access the file.    
                String fieldValue = item.getName();                                             

                if(!item.isFormField()) {// formField                               

                      File f = new File(fieldValue);                        

                    // Prepare streams.
                    fs = new FileInputStream(f);
                    fos = new FileOutputStream(complete_path+"\\newvideo.avi");

                        // Write file contents to response.
                        byte[] buffer = new byte[(int)f.length()];
                        int length;
                        while ((length = fs.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }

                    fs.close();
                    fos.close();                        

              }//formField                                                   
            }//while
katura
  • 2,177
  • 14
  • 39
  • 48
  • You say you've already got code that works for images - have you tried adapting it for video files? – no.good.at.coding May 03 '11 at 20:01
  • possible duplicate of [JSP Uploading and downloading Video](http://stackoverflow.com/questions/2930240/jsp-uploading-and-downloading-video) – no.good.at.coding May 03 '11 at 20:02
  • I haven't tried adapting it for video upload...what would have to be adapted??? – katura May 03 '11 at 20:04
  • Can't say without seeing what you have already but it's going to be pretty much the same thing. Why don't you try it and post back with what doesn't work? – no.good.at.coding May 03 '11 at 20:06
  • I can't proceed with trying it out because after looking over my code I've noticed that I'm using BufferedImage and ImageIO.read() and ImageIO.write(RenderedImage im, String formatName, ImageOutputStream output) for uploading image files. What do I use for uploading a video? – katura May 03 '11 at 20:23
  • Well then you're doing it wrong - it looks like you've written code that assumes you're getting an image in the input stream where as the proper way to do this would be to treat each upload as just a file, not a specific type of file. That way, users can upload any type of file not just those you've considered. Please see the question I had linked to as duplicate. Also, please post relevant code and/or check out how to use a file upload library such as FileUpload from Apache Commons. – no.good.at.coding May 03 '11 at 22:22

1 Answers1

0

See JSP Uploading and downloading Video

But keep in mind that the basic principle for a file upload to a servlet remains the same so you might find it worth your time to read this as well - Managing file uploads in JSP/Servlets

The way you've mentioned you're handling image uploads isn't very clean nor is it very efficient. You're creating objects in Java that you're then to writing to disk. There is no need for this overhead, you could simply write the input stream to the disk and get the same results. This also makes it trivial to accept uploads of any kind of file.

Community
  • 1
  • 1
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • My code takes a file upload (images only) and resizes the image (if necessary) **before** putting it to disk. That is why I designed my code the way I did. I didn't want any other type of file coming in, just image files...but now I want to expand, and allow for video. So I will take a look into the links you have provided. I'll post an update soon. – katura May 04 '11 at 13:01
  • **Update** -- Uploading the file was actually pretty simple once I got the code figured out. Thank you:) [code snippet has been posted in my original post above] Now I have to figure out how (with html? some other code?) to allow the user to view the video.... I suppose, if I need to, I should start a separate thread for this??? – katura May 04 '11 at 14:29
  • Ah, good! And I see you've done it using an upload library, which definitely makes things easier and much more robust than writing your own code. As for serving the video, it depends on the video codec and if you're serving up pages < HTML5 or not. You have the options of embedding a flash player or some other player. In any case, you will probably need to transcode the videos if you want to allow uploads of different video file-types in order to use a single video player to stream the video. – no.good.at.coding May 04 '11 at 16:41
  • Thank you for your help, I really appreciate it. I'm looking into the video player's --- there is so much to learn --- probably going to be starting up a new thread on this soon... thanks again! – katura May 05 '11 at 16:12