在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  Python/ struts2中文件上傳 不能更換上傳的文件

struts2中文件上傳 不能更換上傳的文件

使用strust2上傳文件遇到這樣的問題:
html部分代碼如下:

<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){
                $("#message").html(data);
            }
        })
    })
});
</script>
</body>

后臺java代碼:

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;
    }
}

第一次上傳文件,能夠成功:

圖片描述圖片描述

如果我不想上傳這個文件,或者上傳這個文件之后,選擇其它文件再次上傳時,總是上傳第一次的文件,而不上傳新選擇的文件
圖片描述圖片描述

不知道為什么會出現(xiàn)這樣的問題,百度了好久都沒人遇到這種情況 ==希望大神幫我看看這是為什么 ?

回答
編輯回答
墨沫

用開發(fā)者工具看看請求Request Payload

2017年10月16日 12:55
編輯回答
臭榴蓮

試試把formdata放function里

$(function(){
    $("input[name='submit']").click(function(){
        var formData = new FormData();
        formData.append("author",$("input[name='author']").val());
        formData.append("file",$("input[name='file']")[0].files[0]);
        $.ajax({
            url:'uploadfiles',
            type:'post',
            data:formData,
            processData:false,
            contentType:false,
            success:function(data){
                $("#message").html(data);
            }
        })
    })
});
2017年8月28日 20:37