SpringMVC当中请给出一个下载的例子,文件名必需是中文

文件下载
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



例4.1:

<%@ page contentType="text/html; charset=GBK" %>
<html>
<body >
<A href="http://localhost:8080/SpringMVC1/DownLoad.do?file=ibatis环境搭建.ppt">下 载ibatis环境搭建.ppt</A>
</body>
</html>





package com;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@Controller
public class HelloWorldController implements ServletConfigAware,ServletContextAware {
    private ServletContext servletContext;
    @Override
    public void setServletContext(ServletContext arg0) {
        this.servletContext = arg0;
    }
    private ServletConfig servletConfig;
    @Override
    public void setServletConfig(ServletConfig arg0) {
        this.servletConfig = arg0;
    }

    @RequestMapping("/DownLoad.do")
    public void downloadF(HttpServletRequest request, HttpServletResponse response,
            HttpSession sesssion)  throws IOException, ServletException {
        java.io.BufferedInputStream bis = null;
        java.io.BufferedOutputStream bos = null;
        try {
            String filenameiso = request.getParameter("file");
            System.out.println("filenameiso is " + filenameiso);
            String filenamegbk = new String(filenameiso.getBytes("iso8859-1"), "GBK");
            System.out.println("filenameutf is " + filenamegbk);
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-disposition", "attachment; filename="
                    + filenameiso);
            System.out.println("after setHeader");
            bis = new java.io.BufferedInputStream(new java.io.FileInputStream(
                    servletContext.getRealPath("file/" + filenamegbk)));
            bos = new java.io.BufferedOutputStream(response.getOutputStream());
            System.out.println("after new bis bos ");
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            System.out.println("after while ");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        System.out.println("finish ");
    }
}