spring mvc 实现任意文件上传—— 下载<二>

处理上传的核心代码

MultipartHttpServletRequest re=(MultipartHttpServletRequest)request;

//转换request为MultipartHttpServletRequest

Map map=re.getFile();

//key 是input框中的 name 属性 value 得到的MultipartFile对象

List filelist =new ArrayList();

for( Object obj :map.values()){

MultipartFile file=(MultipartFile)obj;

fileList.add(file);

}

//对 文 件做一些过滤 例如 上传文件的类型

//对文件名的修改 等等 都

开始上传

if(filelist.size()>0){

for(int i=0; i<filelist.size();i++){

MultipartFile fj=filelist.get(i);

File uploadfujian =newFile(Constant.FILE_UPLOAD_LENDINGORDERDETAIL_FOLDER+"\\"+lendingOrder.getBczcdh());//构件一个上传路径

if(!uploadfujian.exists()){

uploadfujian.mkdir();//如果构件文件路径不存在 则创建一个

}

String fujianpath=uploadfujian+"\\"+fj.getOriginalFilename();//附件if(fj!=null&&!fj.getOriginalFilename().trim().equals("")){

FileOutputStream outfujian = new FileOutputStream(fujianpath);

FileCopyUtils.copy(fj.getBytes(), outfujian);//上传代码

outfujian.close(); }}}

文件下载

从页面传一个文件名字 或许路径

href=\"lendingorder.do? action=download&fileName="+request.getAttribute("bczcdh")+"\\"+filenames.get(<a i)+"\"/>"+filenames.get(i)+"</a>");

请求的action 代码

@RequestMapping(params="action=download")

public String downLoad(HttpServletRequest request)

{String file=request.getParameter("fileName");

request.setAttribute("filename",Constant.FILE_UPLOAD_LENDINGORDERDETAIL_FOLDER+"\\"+file);

return "common/downloadfile";

}

Downloadfile.Jsp 内容为:

<%@page import="com.jspsmart.upload.SmartUpload"%><%@ page contentType="text/html;charset=UTF-8"%>

<% String f = (String)request.getAttribute("filename");

com.jspsmart.upload.SmartUpload su = new com.jspsmart.upload.SmartUpload();

su.initialize(pageContext);

su.setContentDisposition(null);

su.downloadFile(f);

out.clear();

out=pageContext.pushBody();

%>

你可能感兴趣的:(spring,mvc,String,upload,action,download)