博客
关于我
利用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 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>