Asynchronous Upload of MultipartFile in Spring

Asked 1 years ago, Updated 1 years ago, 114 views

I saw a lot of similar articles, but I couldn't solve them after trying them all, so please let me know.

Spring 4
Java 8
WTPServer

Asynchronous upload of input type="file" in an environment using

Some commented that Ajax cannot be sent due to security reasons.
Because they were in the minority, they put their credibility on it.
Since the transmission in form.submit() has been successful, we will skip the Spring settings.

view.jsp

<input type="file" id="image_upload">

upload.js

$("#image_upload").change(function(){
    var fd = new FormData();
    varfile=$("#thumbnail_appender").prop("files")[0];
    var name = file.name;
    var size = file.size;

    fd.append("file", file);
    fd.append("name", name);
    fd.append("size", size);

    $.ajax({
        url: http://localhost:8080/upload,
        type: "POST",
        dataType: "text",
        processData: false,
        contentType: false,
        data: fd,
        success:function(data){
            alert(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if(callBack!=null){
                return callBack (textStatus);
            }
        }
    });
});

UploadController.java

@RequestMapping(value="/upload", method=RequestMethod.POST, processes="*/*")
public String thumbnailChange(@RequestParam("name") String name,
                              @RequestParam("size") long size, 
                              HttpServletRequest request,
                              HttpServletResponse response)throws Exception {
        Collection <Part> parts=request.getParts();
        Part = parts.iterator().next();
        File inputFile=(File)ReflectionUtils.select(ReflectionUtils.select(part, "fileItem", "tempFile");
        BufferedImage bufferedImage=ImageIO.read(inputFile);

        String[]nameSep=name.split("\\.");
        String outputFile=SAVE_DIRECTORY+name;

        ImageIO.write(bufferedImage,nameSep[name.length()-1],newFile(outputFile)));
        return outputFile.getName();
}

UploadController

@RequestParam("file") MultipartFile multipartFile

If you set the , you will see そんなNo such parameters have arrived. と 来て 」 よません パラメータ 。 する· 設定 なります を 「
The HttpServletRequest appears to contain parameters, but
Even when I dumped it, there was no important binary data.
It appears to be temporarily stored in the application context with .tmp data.
ApplicationPart.fileItem.tempFile had a full path string.
*There was no file pointed to by that pass, so I think it was made when I was doing a different test.

I'd like to process binary image data and metadata on the server side.
Are there any misconfiguration?

jquery ajax spring java8 mvc

2022-09-30 20:22

1 Answers

In the implementation described in the question, the header information in the file was acceptable, but the binary data itself was not sent.

Controller

@Controller
@MultipartConfig (fileSizeThreshold=1024*1024, maxFileSize=10*1024*1024, maxRequestSize=10*1024*1024)
public class maintenanceController extensions CommonsExceptionHandler {

So I was able to receive it.


2022-09-30 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.