中文文件下载

2.中文文件下载:
假 如你的文件名是英文的话,jspsmartupload就可以胜任。但如果你的文件名是中文的话,就只能用下面的方法。这时jspsmartupload 是不能胜任的。以下例子能下载中文文件名(在firefox,ie8,360都通过测试, eclipse内置浏览器不行):

例 1.2.1
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body >
<A href="http://localhost:8080/ServletHello/MarkToWinServlet?file=ibatis环境搭建.ppt">下 载ibatis环境搭建.ppt</A>
</body>
</html>
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException  {
        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(
                    getServletContext().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 ");

更多请见:http://www.mark-to-win.com/index.html?content=Jsp/jspUrl.html&chapter=Jsp/jsp8_web.html#ChineseDownload

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值