Uploading files in struts2 cannot replace uploaded files

there is a problem with uploading files using strust2:
html part of the code is as follows:

<body>
    <input type="file" name="file"><br>
    <input type="text" name="author">
    <input type="button" name="submit" value="">
    <div id="message"></div>
<script src="jquery-3.1.1.min.js"></script>
<script>
$(function(){
    var formData = new FormData();
    formData.append("author",$("input[name="author"]").val());
    formData.append("file",$("input[name="file"]")[0].files[0]);
    $("input[name="submit"]").click(function(){
        $.ajax({
            url:"uploadfiles",
            type:"post",
            data:formData,
            processData:false,
            contentType:false,
            success:function(data){
                $("-sharpmessage").html(data);
            }
        })
    })
});
</script>
</body>

background java code:

public class UploadAction extends ActionSupport{
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    private static final long serialVersionUID = 1L;
    
    private File file; //
    private String fileContentType; //
    private String fileFileName; //
    private String author;//
    
    public String upload(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        System.out.println(":"+this.getFileFileName());
        System.out.println(":"+this.getFileContentType());
        System.out.println(":"+this.getFile());
        System.out.println(":"+this.getAuthor());
        
        String realPath=ServletActionContext.getServletContext().getRealPath("/upload");
        File file_dest = new File(realPath);
        
        if(!file_dest.exists()) file_dest.mkdir();
        
        try {
//            FileUtils.copyFile(file, new File(file_dest,fileFileName));
            FileInputStream is = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(realPath+"/"+fileFileName);
            byte[] buf = new byte[1024*1024];
            int len = 0;
            while((len = is.read(buf))!=-1){
                fos.write(buf,0,len);
            }
            is.close();
            fos.close();
            response.getWriter().write(fileFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return NONE;
    }
}

the first time you upload a file, you can upload it successfully:


I don"t know why there is such a problem. No one in Baidu has encountered this situation for a long time. I hope God will help me see why?

Mar.01,2021

try putting formdata in function

  

use developer tools to see the request Request Payload

Menu