wood burning stoves 2.0*
The moose likes JDBC and the fly likes how to upload files to My sql with jsp 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 » Databases » JDBC
Bookmark "how to upload files to My sql with jsp" Watch "how to upload files to My sql with jsp" New topic
Author

how to upload files to My sql with jsp

P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 118
Hi


I got idea to do file upload in servlet but the main thing is iam haveing two jar file it is essential to upload file, i don't know where i want to save the file.


Iam useing Eclips to develop, i have stored the jar files in web-INF\lib folder.
this is my servlet code


import java.io.*;
import java.util.*;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.output.*;

public class UploadServlet extends HttpServlet {

private boolean isMultipart;
private String filePath;
private int maxFileSize = 50 * 1024;
private int maxMemSize = 4 * 1024;
private File file ;

public void init( ){
// Get the file location where it would be stored.
filePath =
getServletContext().getInitParameter("file-upload");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {
// Check that we have a file upload request
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );

try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);

// Process the uploaded file items
Iterator i = fileItems.iterator();

out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException {

throw new ServletException("GET method used with " +
getClass( ).getName( )+": POST method required.");
}
}


While iam compileing time iam getting error, and from some tutorial its telling to set the class path i don't know to where i need to store the jar file and how to set the path, because i need servlet class file.


Please guide me, if i did any mistake in servlet also please inform me.


Thanks in advance.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

added code tags and indentation for you.
P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 118
Hi

Thank you very much i have import some files its working fine.

P Arunkumar
Ranch Hand

Joined: Jan 05, 2012
Posts: 118
Hi


Still iam facing problem in upload. please check my servlet code while compilation no problem. while running file is not storing.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to upload files to My sql with jsp
 
Similar Threads
problem with upload file using common file upload servlet
Client Issue
java mail:send data from text box and attachment file from jsp page to mail.
Apache commons file upload does not work for me.
how to upload using html or javascript?