0

i am sending both files and other fields data to servlet using ajax FormData append() as follows:

html

<form   id="formId1" >
   <input type="file" name="file" id="fileid">
   <input type="text" name="t1" id="d">
   <input type="submit" id="btn" value="submit">
</form>

ajax call:

  $("#btn").click(function(event){ 
                  event.preventDefault(); 
                  var fd = new FormData();
      var other_data = $('form').serializeArray();
                    $.each(other_data,function(key,input){
                        fd.append("t1",$("#d").val());
                      fd.append("file",$('#fileid').prop('files')[0])
                    });
                    $.ajax({
                        url: 'Sample1',
                        data: fd,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function(data){
                            console.log(data);
                        }
                    });     });

and i used @MultipartConfig to read both files and other data

@WebServlet("/Sample1")
@MultipartConfig
public class Sample1 extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    { 

        String s=request.getParameter("t1");
        System.out.println(s); 
        Part part=request.getPart("file");
        File path =new File("path")// HOW TO PUT PART INTO THE FOLDER
        }}

now am not able to store that part as image into the folder how to get folder or images from Part please help me this.

raj
  • 19
  • 1
  • 7
  • This might be helpful : https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824. In particular "To browse and select a file for upload you need a HTML field in the form. As stated in the HTML specification you have to use the POST method and the enctype attribute of the form has to be set to "multipart/form-data"" – Tom Mac Oct 27 '17 at 10:17
  • thanks for the suggestion @ Tom Mac in that link ,its not showing how to store image to folder – raj Oct 27 '17 at 10:49

1 Answers1

0

this code will store file into destination folder

@WebServlet("/Sample1")
@MultipartConfig
public class Sample1 extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    { 

                String jj=request.getParameter("t1");
                System.out.println(jj); 


            //GETTING FILE
                Part filePart = request.getPart("file"); 
            //GETTING FILE NAME
                String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();  

                InputStream fileContent = filePart.getInputStream();

                    File file1 =new File("D:\\Projects\\i_seva\\WebContent\\"+fileName+"."+"png");
                    BufferedImage imBuff = ImageIO.read(fileContent);

                    ImageIO.write(imBuff, "png", file1); 

                    System.out.println(fileName); 

    }   

}

thank you friends i did it with help of stack over flow members suggested links and posts

raj
  • 19
  • 1
  • 7