angularjsFileUpload+Springmvc上传文件

6 篇文章 0 订阅
2 篇文章 0 订阅

1.html页面以及js来自于angularjsFileUpload的demo

multiple属性为允许多个文件同时上传


参数设置

url为文件上传路径

formData属性对应的是参数数组,这里以传递id为例


2.springmvc后台

package com.baosight.webapp.web;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.baosight.webapp.bean.App;



@Controller
@RequestMapping("/ngFileUpload")
public class NgFileUpload {

	@RequestMapping(value="/test", method=RequestMethod.POST	)
	@ResponseBody
	public Map<String, Object> ngUpload(HttpServletRequest request,HttpServletResponse res){
		System.out.println("in");
		//接收参数
		int id= Integer.parseInt(request.getParameter("id"));
		System.out.println("id=="+id);
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("result", "error");

		//解析器解析request的上下文
		CommonsMultipartResolver multipartResolver =
			new CommonsMultipartResolver(request.getSession().getServletContext());
		  //先判断request中是否包涵multipart类型的数据,
		if(multipartResolver.isMultipart(request)){
		   //再将request中的数据转化成multipart类型的数据
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator iter = multiRequest.getFileNames();
			while(iter.hasNext()){
				//这里的name为fileItem的alias属性值,相当于form表单中name
				String name=(String)iter.next();
				System.out.println("name:"+name);
				//根据name值拿取文件
				MultipartFile file = multiRequest.getFile(name);
				if(file != null){
					String fileName = file.getOriginalFilename();
					String path = "D:/test/" + fileName;
					File localFile = new File(path);
					if(!localFile.getParentFile().exists()) {
						 //如果目标文件所在的目录不存在,则创建父目录
						 localFile.getParentFile().mkdirs();
						 System.out.println("parent:"+localFile.getParentFile().getPath());
					 }
					//写文件到本地
					try {
						file.transferTo(localFile);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return resMap;
					}
				}
			}
		  }else {

			  return resMap;
		}
		  resMap.put("result", "success");
		  return resMap;
    }


}

3.备注:

获取参数 request.getParameter("id")
这里的拿到的name为fileItem的alias属性值,相当于form表单中name           String name=(String)iter.next();
如果想通过file的name属性值获取文件,对文件进行分类接受,可以通过controller中item的alias属性值。

uploader.uploadAll()是提交所有文件的方法,但是实质上来说该方法是将每个文件提交一次。虽然只触发了一次uploadAll方法,但是有多少个文件就会进多少次后台。实质上是文件进行单个传输。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值