How to upload image to a specified project folder using servlet?

12.5k views Asked by At
<body>
    <form method="POST" action="FileUpload" enctype="multipart/form-data" >
        File:
        <input type="file" name="fileSrc"  > <br/>
        <input type="submit" value="Upload" name="upload" >
    </form>
</body>

This is my UploadImg.jsp ,when i click on Upload it goes to FileUpload.java where the uploaded image has to store in my specified folder AppImages,how can i do this? thanks for your help.

1

There are 1 answers

0
AudioBubble On
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
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.Part;


@WebServlet("/FileUploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
             maxFileSize=1024*1024*10,      // 10MB
             maxRequestSize=1024*1024*50)

public class FileUploadServlet extends HttpServlet {
private static final String SAVE_DIR="images";

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, ClassNotFoundException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
        String savePath = "C:" + File.separator + SAVE_DIR; //specify your path here
            File fileSaveDir=new File(savePath);
            if(!fileSaveDir.exists()){
                fileSaveDir.mkdir();
            }

        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);
    }*/
      //you can change this part acc. to your requirements
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/UploadFile","root","root");
        String query="INSERT INTO table_name(file) values (?)";

            PreparedStatement pst;
            pst=con.prepareStatement(query);

            String filePath= savePath + File.separator + fileName ;
            pst.setString(1,filePath);
            pst.executeUpdate();
}
// 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 "";
}
}

In this servlet, we use two annotations: @WebServlet: marks this servlet so that the servlet container will load it at startup, and map it to the URL pattern /FileUploadServlet. @MultipartConfig: indicates this servlet will handle multipart request. We restrict maximum size of the upload file up to 16 MB.

Source