how to upload files using multipart form in jsp. i have tried uploading and the file is getting uploaded but the other fields in the form like text/dropdown box,their value cannot be read
Asked
Active
Viewed 3,765 times
2 Answers
0
I am sure the below code work fine because well tested.
JSP file:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="POST" action="UploadServlet" enctype="multipart/form-data" >
File:
<input type="file" name="file" id="file" /> <br/>
Destination:
<input type="text" name="foldername"/>
</br>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>
</html>
Servlet file:
package com.servicehandler;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Part;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2,//2mb
maxFileSize=1024*1024*10,//10mb
maxRequestSize=1024*1024*50)//50mb
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
String subfoldername=request.getParameter("foldername");
String realpath=getServletContext().getRealPath("/");
uploadFileTolocaldirectory(subfoldername,request);
uploadFileToserverContextRealpath(subfoldername,realpath,request);
uploadFileToapplicationdirectory(subfoldername,request);
}
public static void uploadFileTolocaldirectory(String foldername,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
{
String savePath="C:\\rootdir\\"+File.separator+foldername;
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists())
{
fileSaveDir.mkdirs();
}
Part part =request.getPart("file");
String fileName = extractFileName(part);
part.write(savePath + File.separator + fileName);
}
public static void uploadFileToserverContextRealpath(String foldername,String realpath,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
{
String savepath=realpath+File.separator+foldername;
File fileSaveDir = new File(savepath);
if (!fileSaveDir.exists())
{
fileSaveDir.mkdirs();
}
Part part =request.getPart("file");
String fileName = extractFileName(part);
part.write(savepath + File.separator + fileName);
}
public void uploadFileToapplicationdirectory(String foldername,HttpServletRequest request) throws IllegalStateException, IOException, ServletException
{
String path="D:\\yeldiallproject\\Fileuploadusingservlet\\WebContent";
String savepath=path+File.separator+foldername;
File fileSaveDir = new File(savepath);
if (!fileSaveDir.exists())
{
fileSaveDir.mkdirs();
}
Part part =request.getPart("file");
String fileName = extractFileName(part);
part.write(savepath + File.separator + fileName);
}
private static String extractFileName(Part part) {
String partHeader = part.getHeader("content-disposition");
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
String filename = content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}
}

Gustavo Morales
- 2,614
- 9
- 29
- 37
0
Update
Here's a much better answer along with why what you're trying doesn't work: How to upload files to server using JSP/Servlet?
You must use a library such as Apache Commons FileUpload or com.oreilly.servlet if you're working with a container such as Tomcat 6 (or anywhere where the Servlet 3.0 spec isn't supported).

Community
- 1
- 1

no.good.at.coding
- 20,221
- 2
- 60
- 51
-
there is one more condition on the upload button...its dependent on the value of an input field in the same form.......if there is a value greater than two then only the file upload should be asked...i want to incorporate ajax in this...i will try the solution you provided and get back to you. – anonymous Mar 16 '11 at 04:47