博客
关于我
利用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索引下沉:提升查询性能的隐藏秘
查看>>
MySql索引为什么使用B+树
查看>>
MySQL索引为什么是B+树
查看>>
WARNING!VisualDDK wizard was unable to find any DDK/WDK installed on your system.
查看>>
MySQL索引介绍及百万数据SQL优化实践总结
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>
MySQL索引和查询优化
查看>>
mysql索引底层数据结构和算法
查看>>
Mysql索引底层结构的分析
查看>>
MySQL索引底层:B+树详解
查看>>
Mysql索引总结
查看>>
mysql索引最左匹配原则理解以及常见的sql使用的索引情况的实测
查看>>
Mysql索引类型
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
mysql索引能重复吗_mysql “索引”能重复吗?“唯一索引”与“索引”区别是什么?...
查看>>