• Post Reply Bookmark Topic Watch Topic
  • New Topic

file upload from client to server  RSS feed

 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
can anyone help me pout with the following prob. i need to upload a file and store it on the database or server machine. which is better approach. storing in database or server...
the other prob is how to do that???
thanx a lot in advance.
Amit
 
Pawan Ramchandani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you have to use following APIs from Oreilly
com.oreilly.servlet.MultipartRequest,
com.oreilly.servlet.multipart.DefaultFileRenamePolicy
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't HAVE to use those - there are others.
Brian
 
archana vishwanath
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Storing in the database may not be the better approach.
I will tell you how to upload to the server
Step1:
Download the commons jar file from apache availabe for free download
http://jakarta.apache.org/commons/fileupload/
Step2:
Keep this jar file in your web-inf\lib directory
Step3:
Refer the user guide for sample

For your reference:

//import the basic package
import org.apache.commons.fileupload.*;
//simplest constructor
DiskFileUpload upload = new DiskFileUpload();
//Set upload parameters
int yourMaxMemorySize=9000;
int yourMaxRequestSize=9000;
//Directory in the server to save your files
String yourTempDirectory="d:\\tempdir\\";
upload.setRepositoryPath(yourTempDirectory);
//Save temporarily
List items = upload.parseRequest(request);
//The rest is to scroll through the form elements and to find the file
element and upload the file element permanently.
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
byte[] data = item.get();
FileOutputStream os = new FileOutputStream("d:\\tempdir\\filename.txt");
os.write(data);
}

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!