SpringMVC + jquery.uploadify實現(xiàn)上傳文件功能
前言
以前用Asp.net MVC+uploadify上傳文件,最近學(xué)習(xí)SpringMVC,所以就用SpringMVC+uploadify做個上傳文件的demo。
剛開始用form表單的方式提交,在Controller Action中用@RequestParam MultipartFile file就能拿到上傳文件信息。后我直接使用uploadify的方式上傳,接口沒有做任何調(diào)整,上傳的過程中報http400, 客戶端的請求不符合接口的要求,表單post提交時報文參數(shù)是以Form Data方式,而換成uploadify時參數(shù)格式則是request payload的方式,所以把接口改寫成MultipartServletRequest的方式
開發(fā)環(huán)境
SpringMVC4、Uploadify、
上傳文件的話還需要下載 commons-fileupload ,同時還會下載common-io、common-logging
項目結(jié)構(gòu)

普通表單上傳
<form action="/User/index" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="upload"/> </form>
@RequestMapping("upload")
public @ResponseBody String upload(@RequestParam MultipartFile file) throws IOException {
String path =request.getSession().getServletContext().getRealPath("upload");
File file=new File(path,file.getOriginalFilename());
file.transferTo(file); //保存文件
return "/success";
}
uploadify上傳文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index</title>
<link href="/2sc/uploadify/uploadify.css" rel="external nofollow" rel="stylesheet" type="text/css" />
<script src="/2sc/js/jquery-1.4.2.js" type="text/javascript"></script>
<script src="/2sc/uploadify/jquery.uploadify.js" type="text/javascript"></script>
<style type="text/css">
#fileQueue {position: absolute;bottom: 0;right: 0;}
</style>
</head>
<body>
spring mvc 上傳文件
<div id="fileQueue">
</div>
<input type="file" name="uploadify" id="uploadify" />
<script type="text/javascript">
$(function () {
$("#uploadify").uploadify({
'method':'post',
//指定swf文件
'swf': '/2sc/uploadify/uploadify.swf',
//后臺處理的頁面
'uploader': '/User/upload',
//按鈕顯示的文字
'buttonText': '上傳圖片',
//顯示的高度和寬度,默認(rèn) height 30;width 120
//'height': 15,
//'width': 80,
//上傳文件的類型 默認(rèn)為所有文件 'All Files' ; '*.*'
//在瀏覽窗口底部的文件類型下拉菜單中顯示的文本
'fileTypeDesc': 'Image Files',
//允許上傳的文件后綴
'fileTypeExts': '*.gif; *.jpg; *.png',
//發(fā)送給后臺的其他參數(shù)通過formData指定
'formData': { 'someKey': 'someValue'},
//上傳文件頁面中,你想要用來作為文件隊列的元素的id, 默認(rèn)為false 自動生成, 不帶#
'queueID': 'fileQueue',
//選擇文件后自動上傳
'auto': true,
//設(shè)置為true將允許多文件上傳
'multi': true
});
});
</script>
</body>
</html>
接口
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response){
String path =request.getSession().getServletContext().getRealPath("upload");
MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
Map<String,MultipartFile> map = multipartHttpServletRequest.getFileMap();
System.out.println("path:"+path);
File file=new File(path);
if(!file.exists()){
file.mkdirs();
}
try{
for(Map.Entry<String,MultipartFile> entity:map.entrySet()){
MultipartFile multipartFile=entity.getValue();
File ff = new File(path,multipartFile.getOriginalFilename());
multipartFile.transferTo(ff);
}
return "success";
}catch (Exception e){
e.printStackTrace();
return "error";
}
}

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
spring?kafka?@KafkaListener詳解與使用過程
這篇文章主要介紹了spring-kafka?@KafkaListener詳解與使用,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
SpringBoot使用Validation進行參數(shù)校驗的示例詳解
在 SpringBoot項目開發(fā)中,有一個觀點是不要相信前端傳入的參數(shù),因為你不知道用戶是怎么操作我們接口的,所以在后端也需要對參數(shù)進行校驗,這篇文章主要講講我們項目中最常使用的驗證方案2023-05-05
dubbo如何設(shè)置連接zookeeper權(quán)限
這篇文章主要介紹了dubbo如何設(shè)置連接zookeeper權(quán)限問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Maven默認(rèn)中央倉庫(settings.xml 配置詳解)
這篇文章主要介紹了Maven默認(rèn)中央倉庫(settings.xml 配置詳解),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

