日期:2014-05-19  浏览次数:20655 次

关于 java下载
我实现下载功能的时候;例如路径是http://www.baidu.com/img/baidu_sylogo1.gif
 形式的;不是从当前工程中那东西;从别的服务器拿;读取文件路径会出现(文件名、目录名或卷标语法不正确。)
下面是我的代码:
Java code

String downFilename="http://www.baidu.com/img/baidu_sylogo1.gif";//要下载的文件名称
        String filepath="F:\\commons-httpclient-3.1.jar";//要下载的文件完整路径
        response.setContentType("text/plain");
        response.setHeader("Location",downFilename);
        response.setHeader("Content-Disposition", "attachment; filename=" + downFilename); 
        OutputStream outputStream = response.getOutputStream();
        InputStream inputStream = new FileInputStream(filepath);
        byte[] buffer = new byte[1024];
        int i = -1;
        while ((i = inputStream.read(buffer)) != -1) {
         outputStream.write(buffer, 0, i);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();




InputStream inputStream = new FileInputStream(filepath);
在这一步会出现(文件名、目录名或卷标语法不正确。);
请问如何解决

------解决方案--------------------
String downFilename="http://www.baidu.com/img/baidu_sylogo1.gif";//要下载的文件名称
String filepath="F:\\commons-httpclient-3.1.jar";//要下载的文件完整路径
response.setContentType("text/plain");
response.setHeader("Location",downFilename);
response.setHeader("Content-Disposition", "attachment; filename=" + downFilename); 
InputStream inputStream = response.getInputStream();
OutputStream outputStream = new FileOutputStream(filepath);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
inputStream.close();





------解决方案--------------------
LZ用apache的fileupload下载吧。