I need to upload multiple files at once the code is-
package etourism.ser;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.FilePart;
@WebServlet("/MultipleUpload")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50)
public class MultipleUpload extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String SAVE_DIR="images";
private Connection con;
private PreparedStatement ps;
private PreparedStatement psid;
private ResultSet rs,rsnav;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String savePath = getServletContext().getRealPath("/")/* + File.separator + SAVE_DIR*/;
System.out.println(savePath);
File fileSaveDir=new File(savePath);
if(!fileSaveDir.exists()){
fileSaveDir.mkdir();
}
System.out.println("directory created");
/* String path=getServletContext().getRealPath("/")+"demos";
System.out.println(path);
// HttpSession hs=request.getSession(false);
//String u_id=(String)hs.getAttribute("userinfo");
HttpSession hs=request.getSession(false);
String u_id=(String) hs.getAttribute("userinfo");
String newpath = path+u_id;
System.out.println(newpath);
File f=new File(newpath);
if(!f.exists())
{
f.mkdir();
}
*/
String firstName=request.getParameter("firstname");
Part part=request.getPart("file");
String fileName=extractFileName(part);
part.write(savePath + File.separator + fileName);
/*
//You need this loop if you submitted more than one file
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
part.write(savePath + File.separator + fileName);
}*/
try
{
String strreg="insert into upload values(?,?)";
ps=con.prepareStatement(strreg);
ps.setString(1, firstName);
String filePath= savePath + File.separator + fileName ;
ps.setString(2,filePath);
ps.executeUpdate();
}
catch(Exception e)
{}
}
// file name of the upload file is included in content-disposition header like this:
//form-data; name="dataFile"; filename="PHOTO.JPG"
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
}
kindly help me to run this code bcoz the error occurs is "The requested resource is not available".
What I have tried:
Kindly help me run this code.it occurs resouce not available exception.