spring mvc 上传文件帮助类(留备用)

package com.service.impl;

import com.entity.UploadInfo;
import com.service.UploadHelp;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.common.Prompt;

//import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;

import javax.servlet.ServletContext;
//import javax.imageio.ImageIO;

/**
 * 上传文件帮助类
 * 
 * @author mingqi_QIAN 创建日期2018-10-16
 */
public class UploadHelpImpl implements UploadHelp {

	/**
	 * 上传文件
	 * 
	 * @param file       文件
	 * @param folder     文件夹
	 * @param suffixName 限制的上传扩展名的集合
	 * @param maxSize    限制的最大上传大小
	 * @return prompt
	 */
	@Override
	public Prompt uploadFile(CommonsMultipartFile file, String folder, String[] suffixName, long maxSize) {
		System.out.println(folder);
		Prompt prompt = new Prompt();// 消息提示实体类对象
		String fileName = file.getOriginalFilename();// 获取文件名
		String fileSuffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();// 获取后缀
		String newFileName = String.valueOf(System.currentTimeMillis()) + fileSuffix;// 重命名文件名
		String filePath = getPath(folder).getAbsolutePath();// 获取路径
		System.out.println(filePath);
		String savePath = filePath + "\\" + newFileName;// 保存文件及文件路径
		System.out.println(savePath);
		// 检查文件夹是否在,不存在创建它
		File folders = new File(filePath);
		if (!folders.exists() && !folders.isDirectory()) {
			folders.mkdir();
		}
		// 限制文件上传类型
		long fileSize = file.getSize();
		if (!checkFileSuffix(fileSuffix, suffixName)) {
			prompt.setCode(2);
			prompt.setMessage("请选择允许上传的文件!");
		} else {
			// 限制文件上传大小
			if (fileSize > maxSize) {
				prompt.setCode(3);
				prompt.setMessage("文件超过制限大小!");
			} else {
				try {
					// 写入路径
					file.transferTo(new File(savePath));
					prompt.setCode(1);
					prompt.setMessage("上传成功!");
					// 将文件属性以实体对象形式装
					UploadInfo uploadInfo = new UploadInfo();
					uploadInfo.setFileName(newFileName);
					uploadInfo.setOldFileName(fileName);
					uploadInfo.setFileSize(String.valueOf(fileSize));
					uploadInfo.setFilePath(getPath(folder).getRelativePath()+"/UpLoad/" + folder);
					uploadInfo.setFileSuffixName(fileSuffix);
					// 如果上传的是图片,获取图片的宽高,经测试 此处只支持png获取,jpg会出错
//					String[] images = new String[] { ".jpg", ".gif", ".png",".jpeg",".bmp" };
//					if (Arrays.asList(images).contains(fileSuffix)) {
//						BufferedImage image = ImageIO.read(file.getInputStream());
//						if (image != null) {
//							uploadInfo.setFileWidth(image.getWidth());
//							uploadInfo.setFileHeight(image.getHeight());
//						}
//					}
					// 将文件属性实体加入消息体中
					prompt.setDatas(uploadInfo);
				} catch (Exception e) {
					prompt.setCode(0);
					prompt.setMessage("上传失败");
				}

			}

		}
		return prompt;
	}

	/**
	 * 检查上传文件类型
	 * 
	 * @param filesuffix 文件后缀名
	 * @param suffixName 允许上传的后缀数组
	 * @return
	 */
	private boolean checkFileSuffix(String filesuffix, String suffixName[]) {
		String[] suffixNames = suffixName;
		if (Arrays.asList(suffixNames).contains(filesuffix)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取保存路径
	 * 
	 * @param folder 文件夹
	 * @return
	 */
	private Pathstr getPath(String folder) {
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext.getServletContext();
		Pathstr pathstr = new Pathstr();
		// 得到文件绝对路径
		pathstr.setAbsolutePath(servletContext.getRealPath("/UpLoad/" + folder));
		// 得到文件相对路径
		pathstr.setRelativePath(servletContext.getContextPath());
		return pathstr;
	}
}

/**
 * 取得路径
 * 
 * @author mingqi_QIAN
 *
 */
class Pathstr {
	private String relativePath;// 相对路径
	private String absolutePath;// 绝对路径

	/**
	 * 相对路径
	 * 
	 * @return
	 */
	public String getRelativePath() {
		return relativePath;
	}

	/**
	 * 获得相对路径
	 * 
	 * @param relativePath
	 */
	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	/**
	 * 绝对路径
	 * 
	 * @return
	 */
	public String getAbsolutePath() {
		return absolutePath;
	}

	/**
	 * 获得绝对路径
	 * 
	 * @param absolutePath
	 */
	public void setAbsolutePath(String absolutePath) {
		this.absolutePath = absolutePath;
	}
}

猜你喜欢

转载自blog.csdn.net/qmdweb/article/details/83150299