SmartUpload 中文文件名下载的时候乱码

首先工程要引用com.jspsmart.upload jar包然后新建一下java类

package com.common;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.SmartUploadException;

public class SmartDownload {
 protected byte m_binArray[];
 protected HttpServletRequest m_request;
 protected HttpServletResponse m_response;
 protected ServletContext m_application;
 private boolean m_denyPhysicalPath;

 private String m_contentDisposition;
 

 public SmartDownload() {
  m_denyPhysicalPath = false;
  m_contentDisposition = new String();

 }

 public void setContentDisposition(String contentDisposition) {
  m_contentDisposition = contentDisposition;
 }

 public final void initialize(ServletConfig config,
   HttpServletRequest request, HttpServletResponse response)
   throws ServletException {
  m_application = config.getServletContext();
  m_request = request;
  m_response = response;
 }

 public final void initialize(PageContext pageContext)
   throws ServletException {
  m_application = pageContext.getServletContext();
  m_request = (HttpServletRequest) pageContext.getRequest();
  m_response = (HttpServletResponse) pageContext.getResponse();
 }

 /**
  * @deprecated Method initialize is deprecated
  */

 public final void initialize(ServletContext application,
   HttpSession session, HttpServletRequest request,
   HttpServletResponse response, JspWriter out)
   throws ServletException {
  m_application = application;
  m_request = request;
  m_response = response;
 }

 public void downloadFile(String sourceFilePathName, String contentType,
   String destFileName, String charsetName)
   throws SmartUploadException, IOException, ServletException {
  downloadFile(sourceFilePathName, contentType, destFileName, 65000,
    charsetName);
 }

 public void downloadFile(String sourceFilePathName, String contentType,
   String destFileName, int blockSize, String charsetName)
   throws SmartUploadException, IOException, ServletException {
  if (sourceFilePathName == null)
   throw new IllegalArgumentException(String
     .valueOf((new StringBuffer("File '")).append(
       sourceFilePathName).append("' not found (1040).")));
  if (sourceFilePathName.equals(""))
   throw new IllegalArgumentException(String
     .valueOf((new StringBuffer("File '")).append(
       sourceFilePathName).append("' not found (1040).")));
  if (!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
   throw new SecurityException("Physical path is denied (1035).");
  if (isVirtual(sourceFilePathName))
   sourceFilePathName = m_application.getRealPath(sourceFilePathName);
  File file = new File(sourceFilePathName);
  FileInputStream fileIn = new FileInputStream(file);
  long fileLen = file.length();
  int readBytes = 0;
  int totalRead = 0;
  byte b[] = new byte[blockSize];
  if (contentType == null)
   m_response.setContentType("application/x-msdownload");
  else if (contentType.length() == 0)
   m_response.setContentType("application/x-msdownload");
  else
   m_response.setContentType(contentType);
  m_response.setContentLength((int) fileLen);
  m_contentDisposition = m_contentDisposition == null ? "attachment;"
    : m_contentDisposition;
  if (destFileName == null)
   m_response.setHeader("Content-Disposition", m_contentDisposition
     + " filename="
     + toUtf8String(getFileName(sourceFilePathName)));

  //   m_response.setHeader("Content-Disposition", String
  //     .valueOf((new StringBuffer(String
  //       .valueOf(m_contentDisposition))).append(
  //       " filename=").append(
  //       URLEncoder.encode(getFileName(sourceFilePathName),
  //         charsetName))));
  else if (destFileName.length() == 0)
   m_response.setHeader("Content-Disposition", m_contentDisposition);
  else
   m_response.setHeader("Content-Disposition", m_contentDisposition
     + " filename=" + toUtf8String(destFileName));
  //
  //   m_response.setHeader("Content-Disposition", String
  //     .valueOf((new StringBuffer(String
  //       .valueOf(m_contentDisposition))).append(
  //       " filename=").append(destFileName)));
  while ((long) totalRead < fileLen) {
   readBytes = fileIn.read(b, 0, blockSize);
   totalRead += readBytes;
   m_response.getOutputStream().write(b, 0, readBytes);
  }
  fileIn.close();
 }

 public static String toUtf8String(String s) {
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < s.length(); i++) {
   char c = s.charAt(i);
   if (c >= 0 && c <= 255) {
    sb.append(c);
   } else {
    byte[] b;
    try {
     b = Character.toString(c).getBytes("utf-8");
    } catch (Exception ex) {
     System.out.println(ex);
     b = new byte[0];
    }
    for (int j = 0; j < b.length; j++) {
     int k = b[j];
     if (k < 0)
      k += 256;
     sb.append("%" + Integer.toHexString(k).toUpperCase());
    }
   }
  }
  return sb.toString();
 }

 private String getFileName(String filePathName)
   throws UnsupportedEncodingException {
  int pos = 0;
  filePathName = filePathName.replace('\\', '/');
  if ((pos = filePathName.lastIndexOf('/')) != -1)
   return filePathName.substring(pos + 1);
  else
   return filePathName;
 }

 private boolean isVirtual(String pathName) {
  if (m_application.getRealPath(pathName) != null) {
   File virtualFile = new File(m_application.getRealPath(pathName));
   return virtualFile.exists();
  } else {
   return false;
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值