Javaweb uploads files

Part of the JSP that uploads the file

Uploading files can also be done using form requests to the back end, or ajax requests to the back end

1. Send the request to the back end through the form form

         <form id=”postForm” action=”${pageContext.request.contextPath}/UploadServlet” method=”post” enctype=”multipart/form-data”>

            

                <input type=”text” id=”side-profile-name” name=”username” class=”form-control”>

                <input type=”file” id=”example-file-input” name=”avatar”>

                <button type=”submit” class=”btn btn-effect-ripple btn-primary”>Save

            

        

The improved code does not require the form tag and is implemented directly by the control. Developers only need to focus on the business logic. JS has already sealed it for us

Through the monitoring tool can see the control submitted data, very clear, debugging is also very simple.

2. Send requests to the backend through Ajax

        1.

            $.ajax({ 

                 url : “${pageContext.request.contextPath}/UploadServlet”, 

                 type : “POST”, 

                 data : $( ‘#postForm’).serialize(), 

                 success : function(data) { 

                      $( ‘#serverResponse’).html(data); 

                 }, 

                 error : function(data) { 

                      $( ‘#serverResponse’).html(data.status + ” : ” + data.statusText + ” : ” + data.responseText); 

                 } 

            }); 

Ajax is divided into two parts. One is initialization. The server is notified of the initialization operation through ajax request before the file is uploaded

Sends notifications to the server after a file is uploaded

If the same file exists on the server, the user does not need to upload it again. Instead, the user is notified of the second transmission

Here you can see that the logic of secs is very simple and not particularly complicated.

            var form = new FormData();

            form.append(“username”,”zxj”);

            form.append(“avatar”,file);

            //var form = new FormData($(“#postForm”)[0]);

            $.ajax({

                url:”${pageContext.request.contextPath}/UploadServlet”,

                type:”post”,

                data:form,

                processData:false,

                contentType:false,

                success:function(data){

         

                    console.log(data);

                }

            });

The Java part

Logic for file initialization

Receive file block data. In this logic we receive file block data. Control of the data is optimized for easy debugging. If you use a monitoring tool, you can see the data submitted by the control.

Note:

1. The above Java part of the code can be used directly, just need to upload the picture path and collect data and write the data to the database

2. The file uploaded above uses the byte stream, in fact, can also use other streams, this requires readers to improve the test below

  1. BeanUtils is a tool for assigning attributes to entities

Instead of using Request.getParameter (“”) to retrieve parameters, the request will be parsed directly to determine whether each item is a file or a non-file, and then perform operations accordingly.

The back-end code logic is mostly the same and currently supports MySQL,Oracle, and SQL. Need to configure the database before using, can refer to me to write this article: blog.ncmem.com/wordpress/2…

Welcome to join the group to discuss: 374992201