利用spring 實現文件上傳、下載

org.springframework.util.FileCopyUtils類的copy方法可以實現文件拷貝,同時設置輸出流爲HttpServletResponse,則可以實現文件下載

文件上傳必須使用form的同步或異步表單提交,且設置form屬性enctype="multipart/form-data"

類中filekey爲文件框ID(即下文的fileField
)

前端示例:

<form action="carControll.do?method=bc" method="post" id="form" enctype="multipart/form-data" name="form" onsubmit="return false">
導入EXCEL文件
<input type="file" name="fileField" id="fileField" />
<input name="btn_save_op" type="submit" class="popbtnok01" id="btn_save_op"  value="批量導入" />
</form>



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();
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章