FileAttachment using Servlets And Jsp's
[nobr]HI All
Iam using the following code in jsp to attach file through Servlet,But iam getting only one file attach.But i want Multiple files Attachment Please Help me Give me reply quickly
Very urgent please.....
Kiran
Jsp:
<html>
<FORM ENCTYPE='multipart/form-data' method='POST' action='/SolPortal/uploadFile'>
Select File to Upload :<INPUT TYPE='file' NAME='name'>
<INPUT TYPE='submit' VALUE='upload'>
</FORM>
</html>
****************Servlet is:****************************
package ml.component.admin.service.util;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.fileupload.*;
public class UploadFile extends HttpServlet {
private Connection conn = null;
private Statement stat = null;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
////////files upload:
// first check if the upload request coming in is a multipart request
boolean isMultipart = FileUpload.isMultipartContent(request);
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// parse this request by the handler
// this gives us a list of items from the request
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
// check if the current item is a form field or an uploaded file
if(item.isFormField()) {
// get the name of the field
String fieldName = item.getFieldName();
// if it is name, we can set it in request to thank the user
if(fieldName.equals("name")) {
request.setAttribute("msg", "Thank You: " + item.getString());
// later you can use it like this:
// out.println(request.getAttribute("msg"));
}
PrintWriter out = response.getWriter();
out.println(item.getFieldName() + " = " + item.getString() + "
");
}
else {
// the item must be an uploaded file save it to disk. Note that there
// seems to be a bug in item.getName() as it returns the full path on
// the client's machine for the uploaded file name, instead of the file
// name only. To overcome that, I have used a workaround using
// fullFile.getName().
File fullFile = new File(item.getName());
File savedFile = new File("C:\dump", fullFile.getName());
//String path = getServletContext().getRealPath("/");
try {
item.write(savedFile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println("The file is saved in C:/dum folder
");
out.println("Please Check it...
");
out.println("saved file = " + fullFile.getName());
}
}
}
}
[/nobr]

