wood burning stoves*
The moose likes Servlets and the fly likes Using commons-fileupload-1.2.1.jar to upload file to MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using commons-fileupload-1.2.1.jar to upload file to MySQL" Watch "Using commons-fileupload-1.2.1.jar to upload file to MySQL" New topic
Author

Using commons-fileupload-1.2.1.jar to upload file to MySQL

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
When I click on the link on the Apache site ( http://commons.apache.org/fileupload/using.html )
that is supposed to contain an example ( http://commons.apache.org/fileupload/customizing.html )
of how to use the commons-fileupload-1.2.1.jar to upload a file to a database, I find the page has
not been created yet.

Can anyone recommend a tutorial or a site that has an example of how to use the
commons-fileupload-1.2.1.jar to upload a file to MySQL?

Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
This can be done in two steps

1) upload the file

2) store in database

Please follow the URL to get the complete description of the process.

http://javaatmax.blogspot.com/2009/06/how-to-upload-file-and-save-it-in.html


Praveen
SCJP, SCWCD, SOA
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Thank you so very much, Praveen. My upload works great now; very easy to do.

Do you know how to direct where the download is placed using this method? I had that part working previously with another method,
but I can't seem to figure it out for this procedure.
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi friends..

iam wondering.. which one is faster and good for implementation?..
use binaryStream (which image stored in database) or use img tag (which image stored in web server;s directory)?..

Thanks in advance..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Mary Taylor wrote:Thank you so very much, Praveen. My upload works great now; very easy to do.

Do you know how to direct where the download is placed using this method? I had that part working previously with another method,
but I can't seem to figure it out for this procedure.


Assuming you saved this as a BLOB in the DB, simply fetch the BLOB from the database and stream it to the user.

iam wondering.. which one is faster and good for implementation?..
use binaryStream (which image stored in database) or use img tag (which image stored in web server;s directory)?..


That depends on what type of images your are displaying. I have not stored images in a DB for display on a website, period. Managing the images will become a pain later. Then again a content management software might choose to store entire page fragments as a BLOB in the DB. It depends really.

Does that answer your question ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
I'd like to download the file directly to a directory in the local file system.
I'm not sure what you are referring to by "streaming it to the user". I
do get the file downloaded but to some obscure directory. I just want to
set the directory path where the download is placed.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Mary Taylor wrote:I'd like to download the file directly to a directory in the local file system.
I'm not sure what you are referring to by "streaming it to the user". I
do get the file downloaded but to some obscure directory. I just want to
set the directory path where the download is placed.


Are you saying the file is automatically saved without the browser asking you where to save it ? That is a browser specific property. With firefox you can specify that using Tools -> Options -> Main -> Ask me where to save the files.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Mary, If the question is asked from the perspective of the server the solution is to take the binary stream that you have obtained and simply write to a specific location on the server. Is there an API / method that you are having trouble with implementing this ?
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Actually, this problem is with the file download once the file has been successfully stored in the database.
I used the commons--fileupload package for the upload; but this download code I wrote myself works just
fine except for directing the resting place of the file. I'm sure if I used the commons package for this
part, it would be easy also. Just thought I'd endeavor to complete this first.

Here's the code:



The file gets put in a default Windows Vista selected location, "C:\Users\Username\AppData\Local\Temp". I'd like to direct the location the file is stored in on the download.

Thanks.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Which version of IE are you using ?

The file gets put in a default Windows Vista selected location. I'd like to direct the location the file is stored in on the download.


Have you tried it with firefox ? I am not a vista or IE user so not sure about which settings I would tweak to solve this.
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Using Firefox; but it's a code issue, not a browser setting. At least at one time I had it going
where I wanted it to with code. Just can't seem to find that magic code again!
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 166
Hi Mary
If you are using FireFox, just goto Tools--> Options--> Main Tab.
There you can set the option "Always ask me where to save files".

Also, in your code, close resultset etc in finally block, it is considered a good practice.

Jhakda


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The server cannot direct the browser where to save files: think of the security risks inherent in doing so.

This is a browser setting--browsers will either save to a default directory or ask the user where to save.
Amol Pingate
Greenhorn

Joined: Oct 02, 2008
Posts: 14
Here is sample program


//jsp file
<%@ page language="java" %>
<Html>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<% // for uploading the file we used Encrypt type of multipart/form-data and input of file type to browse and submit the file %>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="Fileupload" METHOD=POST>




<center><center>

PROGRAM FOR UPLOADING THE FILE</center>

Choose the file To Upload: <INPUT NAME="upfile" TYPE="file">

<INPUT TYPE="submit" VALUE="Send File" >


</center>
</FORM>
</BODY>
</HTML>


//Fileupload.java ------ servlet file,also showing path of file


import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;

/**
* Servlet implementation class Fileupload
*/
public class Fileupload extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Fileupload() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try{

boolean isMultipart = FileUpload.isMultipartContent(request);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

Iterator iter = items.iterator();
while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
System.out.println("its a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
File cfile = new File(item.getName());
File tosave = new File(getServletContext().getRealPath("/"),cfile.getName());
item.write(tosave);

response.getWriter().println("Path "+getServletContext().getRealPath(tosave.getName()));
}
}

}catch(Exception e){System.out.println(e);}

}
}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.

(And really? You leave "TODO Auto-generated stub" comments in your code?)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using commons-fileupload-1.2.1.jar to upload file to MySQL
 
Similar Threads
junk characters in netui:form with enctype="multipart/form-data"
Validating an image upload via "Validator-form.xml"
package apache.commons not exists
error during uploading files - no parametres in request
Creating a folder in the server