使用springMVC + ajaxfileupload上传文件

以防忘记,特此记录

在jsp页面引入ajaxfileupload.js文件

下载文件地址  http://download.csdn.net/download/sinat_25712187/10025881

在页面添加标签

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

<button οnclick="uploadFile();">上传<button>

以下是js代码

function uploadFile(){

ajaxFileUpload();

}

function ajaxFileUpload(){
$.ajaxFileUpload({
url: /upload',//用于文件上传的服务器端请求地址
secureuri: false,//是否需要安全协议,一般设置为false
fileElementId: 'file',//文件上传域的ID
dataType: 'json',//返回值类型 一般设置为json
success: function (data){  //服务器成功响应处理函数
if(data.code == "success"){
alert("上传成功");
}else{
alert("上传失败");
return;
}
},
error: function (data, status, e){//服务器响应失败处理函数
alert(e)
return;
}
});
return;
}

以下是后台服务端代码

@RequestMapping("upload")
@ResponseBody
public Map<String, Object> upload(MultipartFile file, HttpServletRequest request){
UploadUtils up = new UploadUtils();
Map<String, Object> map = up.upload(file, request);
String uuid = IdGen.uuid();
map.put("uuid", uuid);
return map;
}

uploadUtils代码

/**
* ajax上传图片
* @param file
* @param mk
* @return
*/
public Map<String, Object> upload(MultipartFile file, HttpServletRequest request){
Map<String, Object> map = new HashMap<String, Object>();

//获取图片信息
String originalFilename = "";
String suffix = "";
if(file.isEmpty()){
map.put("code", "error");
map.put("msg", "图片为空");
return map;
}else{
originalFilename = file.getOriginalFilename();//图片原名
long fileSize = file.getSize();//图片大小
suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();//图片后缀名
if(fileSize > maxSize){
map.put("code", "error");
map.put("msg", "图片过大");
return map;
}
if(!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(suffix)){//判断后缀是否正确  这里可以自己写判断的方法 本人这里用的是项目中的判断方法
map.put("code", "error");
map.put("msg", "请选择正确格式的图片");
return map;
}
}

//创建图片路径
String currentYear = DateUtils.getYear();//当前年
String currentMonth = DateUtils.getMonth();//当前月
String currentTime = DateUtils.getDate("yyyy-MM-dd-HH-mm-ss-SSSS");//当前时间(带时分秒的)这里的dateUtils类需要自己写
String pathdir =File.separator+"files"+File.separator+currentYear+File.separator+currentMonth;//图片需要放在这个文件夹下面
//保存目录的真实路径(磁盘路径)
String path = request.getSession().getServletContext().getRealPath(pathdir);
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
//图片新名字
String newFileName = currentTime+"."+suffix;这里使用年月日时分秒毫秒作为文件的新名字
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, newFileName));//上传文件

//数据库保存路径

String imgUrl =  request.getServletContext().getContextPath() + "/files/"+currentYear+"/"+currentMonth+"/"+currentTime+"."+suffix;
map.put("code", "success");
map.put("name", newFileName);
map.put("msg", imgUrl);
} catch (IOException e) {
System.out.println("文件[" + originalFilename + "]上传失败,堆栈轨迹如下");
            e.printStackTrace();
            map.put("code", "error");
map.put("msg", "上传失败");
}
return map;
}

使用ajaxfileupload.js上传文件需要在tomcat server文件中host中添加虚拟路径 本人这里是这样配的(本人是将文件放到tomcat下的)


结束

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值