这个程序为什么不能下载中文的文件呢...我也改过编码的,可还是不行,,请各位看看      public void read(String filePath, String fileName) {
try {
String fileName1=fileName;
fileName1= URLEncoder.encode(fileName, "UTF-8");
URL ur = new URL("http://localhost:8080/ftpdowns/" +java.net.URLEncoder.encode(fileName1,"UTF-8"));
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.connect();
InputStream is = conn.getInputStream();

// OutputStream os = new FileOutputStream(new File(fileName,
// filePath));
OutputStream os = new FileOutputStream(new File(filePath,fileName1));
byte[] bs = new byte[1024];
int len;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();请哪位大虾给改一下啊.小弟现在头痛.到底是哪里有问题

解决方案 »

  1.   

    试试 fileName 在传入这个方法前转编码,read(filePath,URLEncoder.encode(fileName, "UTF-8")) 
      

  2.   

    应该在action调用前转码就可以
      

  3.   

    楼主把这段代码拿过去看看?我用了BufferedInputStream来获取流。import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;public class DownloadFile { public static void getSong(String _path, String _savePath) {
    String savePath = _savePath;
    String path = _path;
    int BYTE_SIZE = 1;
    int SAVE_SIZE = 1024;
    byte[] buff = new byte[BYTE_SIZE]; //每次读的缓存
    byte[] save = new byte[SAVE_SIZE]; //保存前缓存
    BufferedInputStream bf = null;
    FileOutputStream file;
    URL url = null;
    HttpURLConnection httpUrl;
    try {
    url = new URL(path);
    httpUrl = (HttpURLConnection) url.openConnection();
    System.out.println("已经打开连接....");
    bf = new BufferedInputStream(httpUrl.getInputStream());
    System.out.println("已经获取资源......");
    file = new FileOutputStream(savePath);
    System.out.println("准备保存到:" + savePath); System.out.println("开始读入......");
    int i = 0;
    while (bf.read(buff) != -1) { //一个字节一个字节读
    save[i] = buff[0];
    if (i == SAVE_SIZE - 1) { //达到保存长度时开始保存
    file.write(save, 0, SAVE_SIZE);
    save = new byte[SAVE_SIZE];
    i = 0;
    } else {
    i++;
    }
    }
    //最后这段如果没达到保存长度,需要把前面的保存下来
    if (i > 0) {
    file.write(save, 0, i - 1);
    }
    System.out.println("下载成功!!!");
    httpUrl.disconnect();
    bf.close();
    file.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    DownloadFile
    .getSong(
    "http://www.vv1111.com.cn/199399/musicliao/938.cc_梦醉西楼-陈瑞.wma",
    "D:\\梦.wma");


    }}
      

  4.   

    报的错是这一行代码
     bf = new BufferedInputStream(httpUrl.getInputStream());
      

  5.   

    你看嘛....报的错和以前的一样public void read(String filePath, String fileName) {
            int BYTE_SIZE = 1;
            int SAVE_SIZE = 1024;
            byte[] buff = new byte[BYTE_SIZE]; //每次读的缓存
            byte[] save = new byte[SAVE_SIZE]; //保存前缓存
            BufferedInputStream bf = null;
            FileOutputStream file;
            URL url = null;
            HttpURLConnection httpUrl;
            try {
             url = new URL("http://localhost:8080/ftpdowns/" +fileName);
                httpUrl = (HttpURLConnection) url.openConnection();
                System.out.println("已经打开连接....");
                bf = new BufferedInputStream(httpUrl.getInputStream());
                System.out.println("已经获取资源......");
                file = new FileOutputStream(filePath);
                System.out.println("开始读入......");
                int i = 0;
                while (bf.read(buff) != -1) { //一个字节一个字节读
                    save[i] = buff[0];
                    if (i == SAVE_SIZE - 1) { //达到保存长度时开始保存
                        file.write(save, 0, SAVE_SIZE);
                        save = new byte[SAVE_SIZE];
                        i = 0;
                    } else {
                        i++;
                    }
                }
                //最后这段如果没达到保存长度,需要把前面的保存下来
                if (i > 0) {
                    file.write(save, 0, i - 1);
                }
                System.out.println("下载成功!!!");
                httpUrl.disconnect();
                bf.close();
                file.close();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }fileName传入的文件名是  CDMA网总部新指标体系小时报表$2009-11-29_00-00-00.xls
      

  6.   

    楼主,你是tomcat不?
    Tomcat中支持中文文件名的下载在tomcat文件中找到server.xml文件修改如下内容:红色部分为你要加的。另外,如果你是用eclipse或者Jbuilder2007,你不是在tomcat的安装目录,而要在
    E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf这个里面找到这个server.xml来加这个。我已经解决了,goodluck    <Connector port="8080"  maxHttpHeaderSize="8192"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"               enableLookups="false" redirectPort="8443" acceptCount="100"               connectionTimeout="20000" disableUploadTimeout="true"  URIEncoding="UTF-8"/>
      

  7.   

    你可以在地址栏里面试一下那个文件对应的链接,如果地址栏不能打开或者另存为,说明你的WAS没有启动URI中文编码的Encoding,可能要你的服务器来改了。
      

  8.   

    E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\conf为什么在.plugins下没有org.eclipse.wst.server.core这个文件..
    我把tomcat里的server.xml照你说的改过了.可是还是不行的