栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot文件上传和自定义实体工具

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot文件上传和自定义实体工具


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.web.multipart.MultipartFile;

import com.yuanchuangyun.commons.entity.FileEntity;


public class UploadFileUtils {
	
	
	public static FileEntity uploadFile(HttpServletRequest request,MultipartFile file,String fileBusiness,String userId)throws FileUploadException{
		FileEntity fileEntity = new  FileEntity();
		try {
		//取得当前上传文件的文件名称  
        String myFileName = file.getOriginalFilename();
        //重命名上传后的文件名  
        String suffix = myFileName.substring(myFileName.lastIndexOf("."), myFileName.length());
    	String filePath = request.getSession().getServletContext().getRealPath("upload");
    	Date nowDate = new Date();
    	String businessPuth = File.separator+userId+File.separator+fileBusiness+File.separator;
    	String newFileName =nowDate.getTime()+suffix;
    	fileEntity.setFilePath(File.separator+"upload"+businessPuth+newFileName);//FileModelServiceImpl.java 同步修改
    	fileEntity.setFileName(myFileName);
    	fileEntity.setSuffix(suffix);
    	File newFile = new File(filePath+businessPuth);
    	if(!newFile.exists()){
    		newFile.mkdirs();
    	}
        //定义上传路径  
        File localFile = new File(filePath+businessPuth+newFileName);  
		file.transferTo(localFile);
		} catch (IllegalStateException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new FileUploadException("IllegalStateException",e);
			
		} catch (IOException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new FileUploadException("IOException",e);
		} 
		return fileEntity;
	}
	
	
	public static List uploadFiles(HttpServletRequest request,MultipartFile[] files,String fileBusiness,String userId)throws FileUploadException{
		List fileEntitys= new ArrayList();
		try {
			for(MultipartFile file : files){
				FileEntity fileEntity = new  FileEntity();
				//取得当前上传文件的文件名称  
		        String myFileName = file.getOriginalFilename();
		        //重命名上传后的文件名  
		        String suffix = myFileName.substring(myFileName.lastIndexOf("."), myFileName.length());
		    	String filePath = request.getSession().getServletContext().getRealPath("upload");
		    	Date nowDate = new Date();
		    	String businessPuth = File.separator+userId+File.separator+fileBusiness+File.separator;
		    	String newFileName =nowDate.getTime()+suffix;
		    	fileEntity.setFilePath(File.separator+"upload"+businessPuth+newFileName);
		    	fileEntity.setFileName(myFileName);
		    	fileEntity.setSuffix(suffix);
		    	File newFile = new File(filePath+businessPuth);
		    	if(!newFile.exists()){
		    		newFile.mkdirs();
		    	}
		        //定义上传路径  
		        File localFile = new File(filePath+businessPuth+newFileName);  
				file.transferTo(localFile);
				fileEntitys.add(fileEntity);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new FileUploadException("IllegalStateException",e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new FileUploadException("IOException",e);
		} 
		return fileEntitys;
	}
	
	
	public static String getBaseUploadLocation(HttpServletRequest request){
		return request.getSession().getServletContext().getRealPath(File.separator);
	}
}

文件实体封装 



public class FileEntity implements java.io.Serializable{

	
	
	private static final long serialVersionUID = 3835833847440273964L;
	
	
	private String fileName;
	
	
	private String suffix;
	
	
	private String filePath;

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981165.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号