springmvc 文件上传

前端:springmvc 文件上传 首先要设置form表单的的type属性为multipart/form-data 文件上传只支持post提交方式,不接受get等方式。

springmvc配置文件:

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
    <!-- <property name="maxUploadSize" value="1048576" />  -->  //这里可以配置文件上传大小等 

   </bean>

后台:

  private void uploadApplyPic(MultipartFile[] file, String applyId,String applyCode,String step,String fileName) {
try {
String[] nameArray={"A1-","A2-","A3-","A4-",
"B1-","B2-","B3-","B4-","B5-","B6-","B7-","B8-",
"C1-","C2-","C3-","C4-","C5-","C6-","C7-","C8-","C9-",
"D1-","D2-","D3-","D4-","D5-","D6-","D7-","D8-","D9-",
"E1-","E2-","E3-","E4-","E5-","E6-","E7-","E8-",
"F1-","F2-","F3-","F4-","F5-","F6-","F7-","F8-",
"G1-","G2-","G3-","G4-","G5-","G6-","G7-",
"H1-","H2-","H3-","H4-","H5-","H6-","H7-","H8-",
"I1-","I2-","I3-","I4-","I5-","I6-","I7-","I8-",
"J1-","J2-","J3-","J4-",
"K1-","K2-","K3-","K4-","K5-","K6-",
"O1-","O2-",
"L1-01","L1-02","L1-03","L1-04","L1-05","L1-06","L1-07","L1-08","L1-09","L1-10","L1-11","L1-12","L1-13","L1-14","L1-15","L1-16","L1-17","L1-18","L1-19","L1-20","L1-21","L1-22","L1-23","L1-24","L1-25","L1-26","L1-27","L1-28",
"L2-","L3-"};


for (MultipartFile multipartFile : file) {
String realPath = AppProperties.get("pic.default.path");
File destFile = new File(realPath + applyCode);
if (!destFile.exists()) {
destFile.mkdirs();
}

if(multipartFile.getOriginalFilename().endsWith(".rar")){
ArrayList<String> fileList = new ArrayList<String>();
File tmpRarFile = new File(realPath + applyCode + "/_tmp.rar");
if(!tmpRarFile.exists()){
tmpRarFile.createNewFile();
}
multipartFile.transferTo(tmpRarFile);
RarUtils.unrar(tmpRarFile, realPath + applyCode, fileList);

for(int i = 0; i < fileList.size(); i++){
String orgFileName = fileList.get(i);
String type = orgFileName.substring(0, 2);
Images list = imagesService.findForApply(applyId, type, orgFileName);
boolean isHasName = true;
for(int h = 0 ; h < nameArray.length ; h++){
if(orgFileName.indexOf(nameArray[h]) == 0){
isHasName = false;
break;
}
}
if(!isHasName){
if(list != null && list.getFileName().equals(orgFileName)){
String baseDir = AppProperties.get("pic.default.url") + applyCode + "/" ;
File temp = new File(baseDir);
if(!temp.exists()){
temp.mkdirs();
}
list.setPath(baseDir + orgFileName);
list.setCreateTime(new Date());
list.setStep(step);
imagesService.update(list);
} else {
Images imagesDTO = new Images();
imagesDTO.setApId(new Long(applyId));
imagesDTO.setCreateTime(new Date());
imagesDTO.setType(type);
imagesDTO.setStep(step);
imagesDTO.setFileName(orgFileName);
imagesDTO.setPath(AppProperties.get("pic.default.url") + applyCode + "/" + orgFileName);
imagesService.create(imagesDTO);
}
}
}

if(tmpRarFile != null){
tmpRarFile.delete();
}
continue;
} else {
File f = new File(destFile.getAbsoluteFile() + "/" + fileName + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")));
if (!f.exists()) {
multipartFile.transferTo(f);
f.createNewFile();
} else {
multipartFile.transferTo(f);
}

String originalFilename = multipartFile.getOriginalFilename();
String type = originalFilename.substring(0, 2);
Images list = imagesService.findForApply(applyId, type, originalFilename);
boolean isHasName = true;
for(int i = 0 ; i < nameArray.length ; i++){
if(originalFilename.indexOf(nameArray[i]) == 0){
isHasName = false;
break;
}
}
if(!isHasName){
if(list != null && list.getFileName().equals(originalFilename)){
list.setPath(AppProperties.get("pic.default.url") + applyCode + "/" + originalFilename);
list.setCreateTime(new Date());
list.setStep(step);
imagesService.update(list);
} else {
Images imagesDTO = new Images();
imagesDTO.setApId(new Long(applyId));
imagesDTO.setCreateTime(new Date());
imagesDTO.setType(type);
imagesDTO.setStep(step);
imagesDTO.setFileName(originalFilename);
imagesDTO.setPath(AppProperties.get("pic.default.url") + applyCode + "/" + fileName);
imagesService.create(imagesDTO);
}
}
}

}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}

}


//反射拿到图片服务器的路径

package com.qcwjr.util.common;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;






public class AppProperties {
    private static Properties props = new Properties();
    static {
        try {
            InputStream is = AppProperties.class.getClassLoader().getResourceAsStream("config/app.properties");
            props.load(is);
        } catch (IOException e) {
        }
    }
    
    public static String get(String key) {
        return props.getProperty(key);
    }
}


这里牵涉到业务方法,自己看着修改一下这个工具。


//具体请求方法 调用工具即可

@RequestMapping("/idCardFront")
@ResponseBody
public Map<String,Object> idCardFront(@RequestParam("idCardFront") MultipartFile[] idCardFront,HttpServletRequest request) throws IllegalStateException, IOException{
Map<String,Object> map = new HashMap<String,Object>();
try {
String applyId  = request.getParameter("apply_id");
String type  = request.getParameter("apply_type");
String applyCode = request.getParameter("applyCode");
uploadApplyPic(idCardFront, applyId,applyCode,type,"B1-01");
map.put("message", "身份证正面上传成功!");
} catch (Exception e) {
map.put("message", "身份证正面上传出现异常!请联系管理员。");
e.printStackTrace();
}
return map;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值