springMVC 文件下载

方法一

package com.clj.test.down.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* <一句话功能简述>
* <功能详细描述>
*
* @author  Administrator
* @version  [版本号, 2014年3月7日]
* @see  [相关类/方法]
* @since  [产品/模块版本]
*/
@Component
@Scope("prototype")
@RequestMapping("/downloadFile")
public class DownloadAction
{

    @RequestMapping("download") 
    public ResponseEntity<byte[]> download() throws IOException { 
        String path="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\springMVC\\WEB-INF\\upload\\图片10(定价后).xlsx";
        File file=new File(path);
        HttpHeaders headers = new HttpHeaders(); 
        String fileName=new String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), 
                                          headers, HttpStatus.CREATED); 
    } 
}




方法二

public class WordView implements View {

    public String contentType = "application/msword";
    private String fileName = "unknow.doc";
    private InputStream istream;
    public String getContentType() {
        return contentType;
    }
    
    public WordView(String fileName, InputStream istream){
        this.fileName = fileName;
        this.istream = istream;
        
    }
    

    /**
     *Spring View的扩展实现
     *@see View
     */
    public void render(Map<String, ?> arg0, HttpServletRequest arg1,
            HttpServletResponse response) throws Exception {
        response.addHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        //response.setContentLength(istream.available());
        response.setContentType(contentType);
        POIFSFileSystem poifs = new POIFSFileSystem();
        DirectoryEntry directory = poifs.getRoot();
        directory.createDocument("WordDocument", this.istream);
        poifs.writeFilesystem(response.getOutputStream());
        response.getOutputStream().close();
    }
    
    
}


上传:
public static String upload(HttpServletRequest request,String fileDir) { 
       
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
//MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
       
        /**页面控件的文件流**/   
        MultipartFile multipartFile = multipartRequest.getFile("file");  
       
        //无文件上传
        if (multipartFile == null || "".equals(multipartFile.getOriginalFilename()))
        return null; 
       
        /**得到图片保存目录的真实路径**/   
        String logoRealPathDir = request.getSession().getServletContext().getRealPath(fileDir);
       
        /**根据真实路径创建目录**/   
        File logoSaveFile = new File(logoRealPathDir);    
        if(!logoSaveFile.exists())    
            logoSaveFile.mkdirs();          

        String dateStr = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());    
       
        /**获取文件的后缀**/   
        String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
       
        String logImageName = dateStr+ suffix;//构建文件名称
       
        /**拼成完整的文件保存路径加文件**/   
        String fileName = logoRealPathDir + "/" + logImageName;               
        File file = new File(fileName);         
       
        try {    
            multipartFile.transferTo(file);    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
       
        return logImageName; 
    }

你可能感兴趣的:(springMVC 文件下载)