• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.

(And really? You leave "TODO Auto-generated stub" comments in your code?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic