通过网络上查阅的资料实现的文件下载功能,下面的测试路径是改过的需要更换正确的下载路径:
这个下载功能有个问题 java代码本身功能实现正常,但是放到web环境中下载的时候回出现下载出错的问题 报错页面信息:1.文件还没有生成 2 。路径问题。通过输出发现路径是没有问题的。难道是因为下载的时候服务器文件还没有生成?希望有哪位前辈遇到过类似的问题能解答解答。
package com.company.item.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
public class FileDownload {
public static final String LOCAL_PATH = "D:/";/* 文件下载后存储的默认目录 */
// 传入待下载文件地址
public static void daowLoad(String fileUrl) {
/* 创建好文件下载需要用到的对象,输入流,输出流,http连接,下载后获取的文件名fileName来接收 */
InputStream in = null;
OutputStream out = null;
HttpURLConnection conn = null;
String fileName = null;
try {
// 初始化连接
URL url = new URL(fileUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
// 获取头文件信息
String disposition = conn.getHeaderField("Content-Disposition");
System.out.println(disposition + "00000000000000000");
if (disposition != null && !"".equals(disposition)) {
// 对头文件信息进行处理获取下载文件名
fileName = disposition.split(";")[1].split("=")[1].replaceAll(
"\"", "");
} else {
// 从地址中获取文件名
fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
}
if (fileName != null && !"".equals(fileName)) {
// 文件名解码
fileName = URLDecoder.decode(fileName, "utf-8");
} else {
// 如果无法获取文件名,则随机生成一个
fileName = "file_" + (int) (Math.random() * 10);
}
System.out.println(fileName + "00000000000000000");
// 读取数据
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
byte[] buffer = new byte[2048];
in = conn.getInputStream();
out = new FileOutputStream(new File(LOCAL_PATH, fileName));/* 将下载的文件通过输出流对象以fileName变量值为名称保存到默认路径 */
int count = 0;
int finished = 0;
int size = conn.getContentLength();
while ((count = in.read(buffer)) != -1) {/* 循环每次读取文件信息 */
if (count != 0) {
out.write(buffer, 0, count);/* 写入输出流 */
finished += count;
} else {
break;
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();/* 关闭对象 */
in.close();
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String[] fileUrl = new String[] {
"http://119.143.88.120:8088/d-form/downloadAction.do?method=download&request=CHnK39H87yYrpoya*COhxYZjFyuvxMoMTxf5ux*G3P*xcVARcXjYRPiW2X1deA8r1Zi9JyQW7EI_%5EbDbFfGfhei",
"http://119.145.88.120:8088/d-orm/downloadAction.do?method=download&request=CHnK39H87yYrpoya*COhxWg6LZrtbfxcCbcsMTEfBSN3MQVt6rnQtfng-KSFUoqTGAg6JsaBot0_%5EbBJcgeChjI",
"http://119.141.88.120:8088/fp-platform/downloadAction.do?method=download&request=CHnK39H87yYrpoya*COhxSOrNWmBiRLgn16Q6YVfDOd3MQVt6rnQtfng-KSFUoqTmAcUY3GEnRQ_%5EbJfDdBegjJ",
"http://119.149.88.120:8088/dp-atform/downloadAction.do?method=download&request=CHnK39H87yYrpoya*COhxQazk7d-KcNf2Tv8NgrLz39JOrPJ3i77Kvng-KSFUoqTdJRkP8LtPxU_%5EeBGeCdIcgg",
"http://119.141.88.120:8088/fp-platform/downloadAction.do?method=download&request=CHnK39H87yYrpoya*COhxYZjFyuvxMoMTxf5ux*G3P*xcVARcXjYRPiW2X1deA8r1Zi9JyQW7EI_%5EbDbFfGfhei" };
for (int i = 0; i < fileUrl.length; i++) {
daowLoad(fileUrl[i]);
}
System.out.println("执行完毕...");
}
}