基于springMVC的文件上传-图片

文件上传有多种工具类可以选择,比如:conmons-FileUpload.jar , springMVC自带的文件上传工具类。在多种工具类里本人觉得transferTo()方法最简便好用。下面是基于它实现的图片上传功能的代码。当然它不仅只限于图片格式。

package com.jjs.app.web.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.mljr.carfinance.crm.app.util.FileUtils;
import com.mljr.carfinance.crm.app.util.Logs;
import com.mljr.carfinance.crm.app.util.RespMsg;

/**
 * 图片上传下载类
 * @author jjs
 *
 */
@RestController
@RequestMapping("/img")
public class UploadImgController extends BaseController{
	
	@Value("${img.upload.dir}")
	private static String imgUploadDir;// 图片上传路径
	
	/**
	 * 图片上传
	 * @param imgFile
	 * @return RespMsg
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/upload")
	@ResponseBody
	public RespMsg upLoad(@RequestParam("imgFile") MultipartFile imgFile) throws IllegalStateException, IOException{
		if(imgFile != null){
			String imgName = imgFile.getOriginalFilename();
			String ext = imgName.substring(imgName.lastIndexOf(".") +1);
			if(ext.equals("jpg") || ext.equals("png") || ext.equals("jpeg")){
				SimpleDateFormat sf  = new SimpleDateFormat("yyyyMMdd");
				String dir = sf.format(new Date()); 
				File dayDir = new File(imgUploadDir + File.separator + dir);
				sf.applyPattern("yyyyMMddHHmmssSSS");
				String newImgName = sf.format(new Date()) + "_" + new Random().nextInt(1000) + "." + ext;
				String  savePath = dayDir + File.separator + newImgName;
				imgFile.transferTo(FileUtils.createFile(new File(savePath)));
				Logs.C.info("图片'"+ imgName +"'上传成功", savePath);
				return success("上传成功");
			}
		}
		return fail("上传失败");
	}
	
	/**
	 * 多图片同时上传
	 * @param imgFile
	 * @return RespMsg
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("/multiUpload")
	@ResponseBody
	public RespMsg upLoad(@RequestParam("imgFile") CommonsMultipartFile[] imgFiles) throws IllegalStateException, IOException{
		if(imgFiles.length != 0){
			for(int i = 0; i < imgFiles.length; i++){
				if(imgFiles[i] != null && imgFiles[i].getSize()!=0){
					String imgName = imgFiles[i].getOriginalFilename();
					String ext = imgName.substring(imgName.lastIndexOf(".") +1);
					if(ext.equals("jpg") || ext.equals("png") || ext.equals("jpeg")){
						SimpleDateFormat sf  = new SimpleDateFormat("yyyyMMdd");
						String dir = sf.format(new Date()); 
						File dayDir = new File(imgUploadDir + File.separator + dir);
						sf.applyPattern("yyyyMMddHHmmssSSS");
						String newImgName = sf.format(new Date()) + "_" + new Random().nextInt(1000) + "." + ext;
						String  savePath = dayDir + File.separator + newImgName;
						imgFiles[i].transferTo(FileUtils.createFile(new File(savePath)));
						Logs.C.info("图片'"+ imgName +"'上传成功", savePath);
					}
				}
			}
			return success("上传成功");
		}
		return fail("上传失败");
	}
	
}


工具类:

package com.jjs.app.util;

import java.io.File;
import java.io.IOException;

/**
 * 文件工具类
 * @author jjs
 *
 */
public class FileUtils {

	public static File createFile(File file) throws IOException {
		if (!file.exists()) {
			makeDir(file.getParentFile());
		}
		file.createNewFile();
		return file;
	}

	public static void makeDir(File dir) {
		if (!dir.getParentFile().exists()) {
			makeDir(dir.getParentFile());
		}
		dir.mkdir();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值