博客
关于我
利用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/

你可能感兴趣的文章
Navicat中怎样将SQLServer的表复制到MySql中
查看>>
navicat创建连接 2002-can‘t connect to server on localhost(10061)且mysql服务已启动问题
查看>>
Navicat可视化界面导入SQL文件生成数据库表
查看>>
Navicat向sqlserver中插入数据时提示:当 IDENTITY_INSERT 设置为 OFF 时,不能向表中的标识列插入显式值
查看>>
Navicat因导入的sql文件中时间数据类型有参数而报错的原因(例:datetime(3))
查看>>
Navicat如何连接MySQL
查看>>
navicat导入.sql文件出错2006- MySQLserver has gone away
查看>>
Navicat导入海量Excel数据到数据库(简易介绍)
查看>>
Navicat工具Oracle数据库复制 or 备用、恢复功能(评论都在谈论需要教)
查看>>
Navicat工具中建立数据库索引
查看>>
navicat工具查看MySQL数据库_表占用容量_占用空间是多少MB---Linux工作笔记048
查看>>
navicat怎么导出和导入数据表
查看>>
Navicat怎样同步两个数据库中的表
查看>>
Navicat怎样筛选数据
查看>>
Navicat报错connection is being used
查看>>
Navicat报错:1045-Access denied for user root@localhost(using passwordYES)
查看>>
Navicat控制mysql用户权限
查看>>
navicat操作mysql中某一张表后, 读表时一直显示正在载入,卡死不动,无法操作
查看>>
Navicat连接mysql 2003 - Can't connect to MySQL server on ' '(10038)
查看>>
Navicat连接mysql数据库中出现的所有问题解决方案(全)
查看>>