springmvc ajax非同步檔案上傳

2021-08-20 01:34:05 字數 2241 閱讀 6405

1、匯入相關jar包

* commons-fileupload-1.3.3.jar

* commons-io-2.5.jar

2、在springmvc-config.xml配置檔案中進行一下配置:

3、使用 @responsebody註解、@responsebody註解

示例**(上傳單個檔案)

//檔案上傳

//上傳

file.transferto(filepath);

//生成縮圖

string smallname = time+"_small_"+file.getoriginalfilename();

file smallpath = new file(path, smallname);

thumbnails.of(filepath).scale(0.2).tofile(smallpath);

message.setcode(1);

message.setinfo(filename);

return message;

}else

}

上傳多個檔案(只需要傳多個引數即可)

//多個檔案上傳

//上傳

file.transferto(filepath);

message.setcode(1);

message.setinfo(filename);

return message;

}if(file1!=null && !file1.isempty() )

//上傳

}

4、前端需要使用ajaxfileupload.js(要配合jeuery1.2以下版本使用)

示例**:

$('#file').change(function() 

},error : function(data, status, e)//伺服器響應失敗處理函式

});});

5、匯入thumbnailator可進行縮圖上傳

示例**:

//上傳

file.transferto(filepath);

//生成縮圖

string smallname = time+"_small_"+file.getoriginalfilename();

file smallpath = new file(path, smallname);

thumbnails.of(filepath).scale(0.2).tofile(smallpath);

message.setcode(1);

message.setinfo(filename);

return message;

}else }

springMVC Ajax實現和原理

在方法上新增 responsebody 註解 新增到方法上的 httpmessageconverter 是 spring3.0 新新增的乙個接 口,負責將請求資訊轉換為乙個物件 型別為 t 將物件 型別為 t 輸出為響應資訊 使用 httpmessageconverter 將請求資訊轉化並繫結到處理...

spring mvc ajax檔案上傳詳解

html id uploadform enctype multipart form data id file type file name file id upload type button uploadbutton form js function submit hrcontro upload ...

springmvc ajax返回資料中文亂碼

springmvc ajax返回資料中文亂碼 問題經常出現,這裡提供兩種解決辦法。這裡是乙個 登入的例子,ajax 驗證使用者名稱和密碼 loginform submit function loginverify data loginform serialize datatype json succ...