我在网上看了很多修改SmartUpload类就可以下载中文名字的文件,但是我在反编译SmartUpload类后然后修改如下:
/*
* 由 ReJava v1.4 反编译
*  Copyright 2005-2007 Apull
* 2007-9-25
* 源文件:SmartUpload.java
*/package com.jspsmart.upload;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;// Referenced classes of package com.jspsmart.upload:
// Files, Request, SmartUploadException, Filepublic class SmartUpload
{ protected byte m_binArray[];
protected javax.servlet.http.HttpServletRequest m_request;
protected javax.servlet.http.HttpServletResponse m_response;
protected javax.servlet.ServletContext m_application;
private int m_totalBytes;
private int m_currentIndex;
private int m_startData;
private int m_endData;
private java.lang.String m_boundary;
private long m_totalMaxFileSize;
private long m_maxFileSize;
private java.util.Vector m_deniedFilesList;
private java.util.Vector m_allowedFilesList;
private boolean m_denyPhysicalPath;
private boolean m_forcePhysicalPath;
private java.lang.String m_contentDisposition;
public static final int SAVE_AUTO = 0;
public static final int SAVE_VIRTUAL = 1;
public static final int SAVE_PHYSICAL = 2;
private com.jspsmart.upload.Files m_files;
private com.jspsmart.upload.Request m_formRequest; public SmartUpload()
{
m_totalBytes = 0;
m_currentIndex = 0;
m_startData = 0;
m_endData = 0;
m_boundary = new String();
m_totalMaxFileSize = 0L;
m_maxFileSize = 0L;
m_deniedFilesList = new Vector();
m_allowedFilesList = new Vector();
m_denyPhysicalPath = false;
m_forcePhysicalPath = false;
m_contentDisposition = new String();
m_files = new Files();
m_formRequest = new Request();
} /**
 * @deprecated Method init is deprecated
 */ public final void init(javax.servlet.ServletConfig config)
throws javax.servlet.ServletException
{
m_application = config.getServletContext();
} /**
 * @deprecated Method service is deprecated
 */ public void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException
{
m_request = request;
m_response = response;
} public final void initialize(javax.servlet.ServletConfig config, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException
{
m_application = config.getServletContext();
m_request = request;
m_response = response;
} public final void initialize(javax.servlet.jsp.PageContext pageContext)
throws javax.servlet.ServletException
{
m_application = pageContext.getServletContext();
m_request = (javax.servlet.http.HttpServletRequest)pageContext.getRequest();
m_response = (javax.servlet.http.HttpServletResponse)pageContext.getResponse();
} /**
 * @deprecated Method initialize is deprecated
 */ public final void initialize(javax.servlet.ServletContext application, javax.servlet.http.HttpSession session, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.jsp.JspWriter out)
throws javax.servlet.ServletException
{
m_application = application;
m_request = request;
m_response = response;
} public void upload()
throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
java.lang.String dataHeader = new String();
java.lang.String fieldName = new String();
java.lang.String fileName = new String();
java.lang.String fileExt = new String();
java.lang.String filePathName = new String();
java.lang.String contentType = new String();
java.lang.String contentDisp = new String();
java.lang.String typeMIME = new String();
java.lang.String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
for (; totalRead < m_totalBytes; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch (java.lang.Exception e)
{
throw new SmartUploadException("Unable to upload.");
} for (; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if (m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex]; if (m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if (m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if (isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if (isFile && fileName.length() > 0)
{
if (m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if (!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if (m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(java.lang.String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if (m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if (isFile)
{
com.jspsmart.upload.File newFile = new com.jspsmart.upload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
newFile.setFileName(fileName);
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if (contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
java.lang.String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
m_formRequest.putParameter(fieldName, value);
}
if ((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while (true);
}

解决方案 »

  1.   

    public int save(java.lang.String destPathName)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    return save(destPathName, 0);
    } public int save(java.lang.String destPathName, int option)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    int count = 0;
    if (destPathName == null)
    destPathName = m_application.getRealPath("/");
    if (destPathName.indexOf("/") != -1)
    {
    if (destPathName.charAt(destPathName.length() - 1) != '/')
    destPathName = java.lang.String.valueOf(destPathName).concat("/");
    } else
    if (destPathName.charAt(destPathName.length() - 1) != '\\')
    destPathName = java.lang.String.valueOf(destPathName).concat("\\");
    for (int i = 0; i < m_files.getCount(); i++)
    if (!m_files.getFile(i).isMissing())
    {
    m_files.getFile(i).saveAs(destPathName + m_files.getFile(i).getFileName(), option);
    count++;
    } return count;
    } public int getSize()
    {
    return m_totalBytes;
    } public byte getBinaryData(int index)
    {
    byte retval;
    try
    {
    retval = m_binArray[index];
    }
    catch (java.lang.Exception e)
    {
    throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
    }
    return retval;
    } public com.jspsmart.upload.Files getFiles()
    {
    return m_files;
    } public com.jspsmart.upload.Request getRequest()
    {
    return m_formRequest;
    } public void downloadFile(java.lang.String sourceFilePathName)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    downloadFile(sourceFilePathName, null, null);
    } public void downloadFile(java.lang.String sourceFilePathName, java.lang.String contentType)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    downloadFile(sourceFilePathName, contentType, null);
    } public void downloadFile(java.lang.String sourceFilePathName, java.lang.String contentType, java.lang.String destFileName)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    downloadFile(sourceFilePathName, contentType, destFileName, 65000);
    } public void downloadFile(String s, String s1, String s2, int i) 
    throws ServletException, IOException, SmartUploadException 

    if(s == null) 
    throw new IllegalArgumentException("File '" + s + 
    "' not found (1040)."); 
    if(s.equals("")) 
    throw new IllegalArgumentException("File '" + s + 
    "' not found (1040)."); 
    if(!isVirtual(s) && m_denyPhysicalPath) 
    throw new SecurityException("Physical path is denied (1035)."); 
    if(isVirtual(s)) 
    s = m_application.getRealPath(s); 
    java.io.File file = new java.io.File(s); 
    FileInputStream fileinputstream = new FileInputStream(file); 
    long l = file.length(); 
    boolean flag = false; 
    int k = 0; 
    byte abyte0[] = new byte; 
    if(s1 == null) 
    m_response.setContentType("application/x-msdownload"); 
    else 
    if(s1.length() == 0) 
    m_response.setContentType("application/x-msdownload"); 
    else 
    m_response.setContentType(s1); 
    m_response.setContentLength((int)l); 
    m_contentDisposition = m_contentDisposition != null ? 
    m_contentDisposition : "attachment;"; 
    if(s2 == null) 
    m_response.setHeader("Content-Disposition", 
    m_contentDisposition + " filename=" + 
    toUtf8String(getFileName(s))); 
    else 
    if(s2.length() == 0) 
    m_response.setHeader("Content-Disposition", 
    m_contentDisposition); 
    else 
    m_response.setHeader("Content-Disposition", 
    m_contentDisposition + " filename=" + toUtf8String(s2)); 
    while((long)k < l) 

    int j = fileinputstream.read(abyte0, 0, i); 
    k += j; 
    m_response.getOutputStream().write(abyte0, 0, j); 

    fileinputstream.close(); 

    public void downloadField(java.sql.ResultSet rs, java.lang.String columnName, java.lang.String contentType, java.lang.String destFileName)
    throws java.sql.SQLException, java.io.IOException, javax.servlet.ServletException
    {
    if (rs == null)
    throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
    if (columnName == null)
    throw new IllegalArgumentException("The columnName cannot be null (1050).");
    if (columnName.length() == 0)
    throw new IllegalArgumentException("The columnName cannot be empty (1055).");
    byte b[] = rs.getBytes(columnName);
    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(b.length);
    if (destFileName == null)
    m_response.setHeader("Content-Disposition", "attachment;");
    else
    if (destFileName.length() == 0)
    m_response.setHeader("Content-Disposition", "attachment;");
    else
    m_response.setHeader("Content-Disposition", "attachment; filename=".concat(java.lang.String.valueOf(destFileName)));
    m_response.getOutputStream().write(b, 0, b.length);
    } public void fieldToFile(java.sql.ResultSet rs, java.lang.String columnName, java.lang.String destFilePathName)
    throws java.sql.SQLException, com.jspsmart.upload.SmartUploadException, java.io.IOException, javax.servlet.ServletException
    {
    try
    {
    if (m_application.getRealPath(destFilePathName) != null)
    destFilePathName = m_application.getRealPath(destFilePathName);
    java.io.InputStream is_data = rs.getBinaryStream(columnName);
    java.io.FileOutputStream file = new FileOutputStream(destFilePathName);
    int c;
    while ((c = is_data.read()) != -1) 
    file.write(c);
    file.close();
    }
    catch (java.lang.Exception e)
    {
    throw new SmartUploadException("Unable to save file from the DataBase (1020).");
    }
    } private java.lang.String getDataFieldValue(java.lang.String dataHeader, java.lang.String fieldName)
    {
    java.lang.String token = new String();
    java.lang.String value = new String();
    int pos = 0;
    int i = 0;
    int start = 0;
    int end = 0;
    token = java.lang.String.valueOf((new StringBuffer(java.lang.String.valueOf(fieldName))).append("=").append('"'));
    pos = dataHeader.indexOf(token);
    if (pos > 0)
    {
    i = pos + token.length();
    start = i;
    token = "\"";
    end = dataHeader.indexOf(token, i);
    if (start > 0 && end > 0)
    value = dataHeader.substring(start, end);
    }
    return value;
    } private java.lang.String getFileExt(java.lang.String fileName)
    {
    java.lang.String value = new String();
    int start = 0;
    int end = 0;
    if (fileName == null)
    return null;
    start = fileName.lastIndexOf('.') + 1;
    end = fileName.length();
    value = fileName.substring(start, end);
    if (fileName.lastIndexOf('.') > 0)
    return value;
    else
    return "";
    } private java.lang.String getContentType(java.lang.String dataHeader)
    {
    java.lang.String token = new String();
    java.lang.String value = new String();
    int start = 0;
    int end = 0;
    token = "Content-Type:";
    start = dataHeader.indexOf(token) + token.length();
    if (start != -1)
    {
    end = dataHeader.length();
    value = dataHeader.substring(start, end);
    }
    return value;
    }
      

  2.   

    private java.lang.String getTypeMIME(java.lang.String ContentType)
    {
    java.lang.String value = new String();
    int pos = 0;
    pos = ContentType.indexOf("/");
    if (pos != -1)
    return ContentType.substring(1, pos);
    else
    return ContentType;
    } private java.lang.String getSubTypeMIME(java.lang.String ContentType)
    {
    java.lang.String value = new String();
    int start = 0;
    int end = 0;
    start = ContentType.indexOf("/") + 1;
    if (start != -1)
    {
    end = ContentType.length();
    return ContentType.substring(start, end);
    } else
    {
    return ContentType;
    }
    } private java.lang.String getContentDisp(java.lang.String dataHeader)
    {
    java.lang.String value = new String();
    int start = 0;
    int end = 0;
    start = dataHeader.indexOf(":") + 1;
    end = dataHeader.indexOf(";");
    value = dataHeader.substring(start, end);
    return value;
    } private void getDataSection()
    {
    boolean found = false;
    java.lang.String dataHeader = new String();
    int searchPos = m_currentIndex;
    int keyPos = 0;
    int boundaryLen = m_boundary.length();
    m_startData = m_currentIndex;
    m_endData = 0;
    do
    {
    if (searchPos >= m_totalBytes)
    break;
    if (m_binArray[searchPos] == (byte)m_boundary.charAt(keyPos))
    {
    if (keyPos == boundaryLen - 1)
    {
    m_endData = ((searchPos - boundaryLen) + 1) - 3;
    break;
    }
    searchPos++;
    keyPos++;
    } else
    {
    searchPos++;
    keyPos = 0;
    }
    } while (true);
    m_currentIndex = m_endData + boundaryLen + 3;
    } private java.lang.String getDataHeader()
    {
    int start = m_currentIndex;
    int end = 0;
    int len = 0;
    boolean found = false;
    while (!found) 
    if (m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
    {
    found = true;
    end = m_currentIndex - 1;
    m_currentIndex = m_currentIndex + 2;
    } else
    {
    m_currentIndex++;
    }
    java.lang.String dataHeader = new String(m_binArray, start, (end - start) + 1);
    return dataHeader;
    } private java.lang.String getFileName(java.lang.String filePathName)
    {
    java.lang.String token = new String();
    java.lang.String value = new String();
    int pos = 0;
    int i = 0;
    int start = 0;
    int end = 0;
    pos = filePathName.lastIndexOf('/');
    if (pos != -1)
    return filePathName.substring(pos + 1, filePathName.length());
    pos = filePathName.lastIndexOf('\\');
    if (pos != -1)
    return filePathName.substring(pos + 1, filePathName.length());
    else
    return filePathName;
    } public void setDeniedFilesList(java.lang.String deniedFilesList)
    throws java.sql.SQLException, java.io.IOException, javax.servlet.ServletException
    {
    java.lang.String ext = "";
    if (deniedFilesList != null)
    {
    ext = "";
    for (int i = 0; i < deniedFilesList.length(); i++)
    if (deniedFilesList.charAt(i) == ',')
    {
    if (!m_deniedFilesList.contains(ext))
    m_deniedFilesList.addElement(ext);
    ext = "";
    } else
    {
    ext = ext + deniedFilesList.charAt(i);
    } if (ext != "")
    m_deniedFilesList.addElement(ext);
    } else
    {
    m_deniedFilesList = null;
    }
    } public void setAllowedFilesList(java.lang.String allowedFilesList)
    {
    java.lang.String ext = "";
    if (allowedFilesList != null)
    {
    ext = "";
    for (int i = 0; i < allowedFilesList.length(); i++)
    if (allowedFilesList.charAt(i) == ',')
    {
    if (!m_allowedFilesList.contains(ext))
    m_allowedFilesList.addElement(ext);
    ext = "";
    } else
    {
    ext = ext + allowedFilesList.charAt(i);
    } if (ext != "")
    m_allowedFilesList.addElement(ext);
    } else
    {
    m_allowedFilesList = null;
    }
    } public void setDenyPhysicalPath(boolean deny)
    {
    m_denyPhysicalPath = deny;
    } public void setForcePhysicalPath(boolean force)
    {
    m_forcePhysicalPath = force;
    } public void setContentDisposition(java.lang.String contentDisposition)
    {
    m_contentDisposition = contentDisposition;
    } public void setTotalMaxFileSize(long totalMaxFileSize)
    {
    m_totalMaxFileSize = totalMaxFileSize;
    } public void setMaxFileSize(long maxFileSize)
    {
    m_maxFileSize = maxFileSize;
    }
      

  3.   

    protected java.lang.String getPhysicalPath(java.lang.String filePathName, int option)
    throws java.io.IOException
    {
    java.lang.String path = new String();
    java.lang.String fileName = new String();
    java.lang.String fileSeparator = new String();
    boolean isPhysical = false;
    fileSeparator = java.lang.System.getProperty("file.separator");
    if (filePathName == null)
    throw new IllegalArgumentException("There is no specified destination file (1140).");
    if (filePathName.equals(""))
    throw new IllegalArgumentException("There is no specified destination file (1140).");
    if (filePathName.lastIndexOf("\\") >= 0)
    {
    path = filePathName.substring(0, filePathName.lastIndexOf("\\"));
    fileName = filePathName.substring(filePathName.lastIndexOf("\\") + 1);
    }
    if (filePathName.lastIndexOf("/") >= 0)
    {
    path = filePathName.substring(0, filePathName.lastIndexOf("/"));
    fileName = filePathName.substring(filePathName.lastIndexOf("/") + 1);
    }
    path = path.length() != 0 ? path : "/";
    java.io.File physicalPath = new File(path);
    if (physicalPath.exists())
    isPhysical = true;
    if (option == 0)
    {
    if (isVirtual(path))
    {
    path = m_application.getRealPath(path);
    if (path.endsWith(fileSeparator))
    path = path + fileName;
    else
    path = java.lang.String.valueOf((new StringBuffer(java.lang.String.valueOf(path))).append(fileSeparator).append(fileName));
    return path;
    }
    if (isPhysical)
    {
    if (m_denyPhysicalPath)
    throw new IllegalArgumentException("Physical path is denied (1125).");
    else
    return filePathName;
    } else
    {
    throw new IllegalArgumentException("This path does not exist (1135).");
    }
    }
    if (option == 1)
    {
    if (isVirtual(path))
    {
    path = m_application.getRealPath(path);
    if (path.endsWith(fileSeparator))
    path = path + fileName;
    else
    path = java.lang.String.valueOf((new StringBuffer(java.lang.String.valueOf(path))).append(fileSeparator).append(fileName));
    return path;
    }
    if (isPhysical)
    throw new IllegalArgumentException("The path is not a virtual path.");
    else
    throw new IllegalArgumentException("This path does not exist (1135).");
    }
    if (option == 2)
    {
    if (isPhysical)
    if (m_denyPhysicalPath)
    throw new IllegalArgumentException("Physical path is denied (1125).");
    else
    return filePathName;
    if (isVirtual(path))
    throw new IllegalArgumentException("The path is not a physical path.");
    else
    throw new IllegalArgumentException("This path does not exist (1135).");
    } else
    {
    return null;
    }
    } public void uploadInFile(java.lang.String destFilePathName)
    throws com.jspsmart.upload.SmartUploadException, java.io.IOException
    {
    int intsize = 0;
    int pos = 0;
    int readBytes = 0;
    if (destFilePathName == null)
    throw new IllegalArgumentException("There is no specified destination file (1025).");
    if (destFilePathName.length() == 0)
    throw new IllegalArgumentException("There is no specified destination file (1025).");
    if (!isVirtual(destFilePathName) && m_denyPhysicalPath)
    throw new SecurityException("Physical path is denied (1035).");
    intsize = m_request.getContentLength();
    m_binArray = new byte[intsize];
    for (; pos < intsize; pos += readBytes)
    try
    {
    readBytes = m_request.getInputStream().read(m_binArray, pos, intsize - pos);
    }
    catch (java.lang.Exception e)
    {
    throw new SmartUploadException("Unable to upload.");
    } if (isVirtual(destFilePathName))
    destFilePathName = m_application.getRealPath(destFilePathName);
    try
    {
    java.io.File file = new File(destFilePathName);
    java.io.FileOutputStream fileOut = new FileOutputStream(file);
    fileOut.write(m_binArray);
    fileOut.close();
    }
    catch (java.lang.Exception e)
    {
    throw new SmartUploadException("The Form cannot be saved in the specified file (1030).");
    }
    } private boolean isVirtual(java.lang.String pathName)
    {
    if (m_application.getRealPath(pathName) != null)
    {
    java.io.File virtualFile = new File(m_application.getRealPath(pathName));
    return virtualFile.exists();
    } else
    {
    return false;
    }
    }

         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();
        }
    }
    就出现这样的错误
    jspsmart\upload\SmartUpload.java:319: '[' expected
    byte abyte0[] = new byte;请各位高手解释一下,帮我改一下,谢谢
      或者谁已经有可以下载中文名字的方法
      给我讲一下,
         [email protected]
      

  4.   

    byte abyte0[] = new byte;
    这样都可以吗?
      

  5.   

    package com.util;import java.io.FileInputStream;
    import java.io.IOException;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.jsp.PageContext;public class DownLoadFile {
    protected byte m_binArray[]; protected HttpServletRequest m_request; protected HttpServletResponse m_response; protected ServletContext m_application; private String m_contentDisposition = null;
    public final void initialize(ServletConfig servletconfig,
    HttpServletRequest httpservletrequest,
    HttpServletResponse httpservletresponse) throws ServletException {
    m_application = servletconfig.getServletContext();
    m_request = httpservletrequest;
    m_response = httpservletresponse;
    } public final void initialize(PageContext pagecontext)
    throws ServletException {
    m_application = pagecontext.getServletContext();
    m_request = (HttpServletRequest) pagecontext.getRequest();
    m_response = (HttpServletResponse) pagecontext.getResponse();
    } public void downloadFile(String s) throws ServletException, IOException {
    //缓存区大小6144字节 6m
    downloadFile(s, 6144);
    } public void downloadFile(String s, int i) throws ServletException,
    IOException {
    if (s == null)
    throw new IllegalArgumentException("File &#39;" + s
    + "&#39; not found (1040).");
    if (s.equals(""))
    throw new IllegalArgumentException("File &#39;" + s
    + "&#39; not found (1040).");
    if (isVirtual(s))
    s = m_application.getRealPath(s);
    else
    return ;
    java.io.File file = new java.io.File(s);
    FileInputStream fileinputstream = new FileInputStream(file);
    long l = file.length();
    if(l<1024)//1k
    i=(int)l;
    int k = 0;
    byte abyte0[] = new byte[i];
    m_response.setContentType("application/x-msdownload");
    m_response.setContentLength((int) l);
    m_contentDisposition = m_contentDisposition != null ? m_contentDisposition
    : "attachment;";
    m_response.setHeader("Content-Disposition", m_contentDisposition
    + " filename=" + toUtf8String(getFileName(s)));
    while ((long) k < l) {
    int j = fileinputstream.read(abyte0, 0, i);
    k += j;
    m_response.getOutputStream().write(abyte0, 0, j);
    }
    fileinputstream.close();
    } private String getFileName(String s) {
    int i = 0;
    i = s.lastIndexOf('/');
    if (i != -1) {
    return s.substring(i + 1, s.length());
    }
    i = s.lastIndexOf('\\');
    if (i != -1) {
    return s.substring(i + 1, s.length());
    } else {
    return s;
    }
    } private boolean isVirtual(String s) {
    if (m_application.getRealPath(s) != null) {
    java.io.File file = new java.io.File(m_application.getRealPath(s));
    return file.exists();
    } else {
    return false;
    }
    } /**
     * 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名. 
     * @param s 原文件名
     * @return 重新编码后的文件名
     */
    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();
    }
    }去年写的你查考下吧我记得中文名没问题,另外这个不好用,推荐用apache的io
    com.util.DownLoadFile dlf = new com.util.DownLoadFile();
    dlf.initialize(getServletConfig(),request,response);
                       dlf.downloadFile(文件);
      

  6.   

    java技术交流,欢迎加入40652189,深入学习java的**群,探讨新技术