博客
关于我
利用spring 实现文件上传、下载
阅读量:506 次
发布时间:2019-03-07

本文共 3581 字,大约阅读时间需要 11 分钟。

org.springframework.util.FileCopyUtils类的copy方法可以实现文件拷贝,同时设置输出流为HttpServletResponse,则可以实现文件下载

文件上传必须使用form的同步或异步表单提交,且设置form属性enctype="multipart/form-data"

类中filekey为文件框ID(即下文的fileField

)

前端示例:

导入EXCEL文件

public class FileStreamService {	public class UploadFileName{		public String allPathName;		public String name ;	}	/**	 * 上传文件	 * 	 * @param request 请求	 * @param fileKey 请求文件所使用的KEY	 * @param DesFileName 目标路径文件名 	 * @return String     全路径文件名 	 * 	 * history	 *	 */	public String fileUpLoad(HttpServletRequest request , String fileKey , String DesFileName){		// 转型为MultipartHttpRequest:		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;		// 获得文件:		CommonsMultipartFile cfile = (CommonsMultipartFile) multipartRequest.getFile(fileKey);		File fo = null;		try {			fo = new File(DesFileName );			cfile.getFileItem().write(fo);		} catch (Exception e) {			throw new SystemException(e.getMessage());		}		return DesFileName;	}		/**	 * 上传文件	 * 	 * @param request	 * @param fileKey	 * @param desFilePath	 * @param DesFileName	 * @return String	 */	public UploadFileName fileUpLoad(HttpServletRequest request, String fileKey, String desFilePath, String DesFileName ){				UploadFileName r = new UploadFileName();				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;		CommonsMultipartFile cfile = (CommonsMultipartFile) multipartRequest.getFile(fileKey);		File dir = new File(desFilePath+File.separator);		if (!dir.exists()){			dir.mkdirs();		}		String fileName = cfile.getOriginalFilename();		String fix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();		fileName = desFilePath+File.separator+DesFileName+fix;		r.allPathName = fileName;		r.name = DesFileName+fix;		File fo = null;		try{			fo = new File(fileName);			cfile.getFileItem().write(fo);		}catch(Exception e){			throw new SystemException(e.getMessage());		}		return r;	}		/**	 * 文件下载	 * 	 * @param response	 * @param filePath 服务器文件路径	 * @param fileName 服务器文件名	 * @param saveFileName 目标文件名	 * @throws IOException	 */	public void fileDownLoad(HttpServletResponse response , String filePath , String fileName , String saveFileName) throws IOException{		InputStream fis = null;		try{			File file = new File(filePath + fileName);			if(!file.exists()){				throw new SystemException("文件不存在");			}			fis = new BufferedInputStream(new FileInputStream(filePath+fileName));			String f = saveFileName.equals("") ? fileName : saveFileName;			response.setContentType("application/x-msdownload;");			response.setHeader("Content-disposition", "attachment; filename="+ new String(f.getBytes("GB2312"), "ISO-8859-1"));			response.setContentType("application/" + fileName.substring(fileName.lastIndexOf(".") + 1));			FileCopyUtils.copy(fis, response.getOutputStream());		}finally{			if(fis != null){				try{					fis.close();				}catch(Exception e){					e.printStackTrace();				}			}		}	}	/**	 * 文件下载	 * @param response 	 * @param fileName 文件URL地址	 * @throws IOException	 */	public void fileDownLoad(HttpServletResponse response ,String fileName) throws IOException	{		String fileAll = FilePatch.getProjectPatch()+File.separator+fileName;		fileAll = fileAll.replace("/", File.separator);		String filepath=fileAll.substring( 0 , fileAll.lastIndexOf(File.separator)+1);		String name=fileAll.substring(fileAll.lastIndexOf(File.separator)+1);		fileDownLoad(response , filepath , name , name);	}	/**	 * 删除文件	 * 	 * @param fileName void	 */	public void fileDel(String fileName){		File file = new File(fileName);		if (file.exists()){			file.delete();		}	}}

转载地址:http://hyrjz.baihongyu.com/

你可能感兴趣的文章
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>