java实现从⽹络下载多个⽂件
java从⽹络下载多个⽂件,供⼤家参考,具体内容如下
⾸先是打包下载多⽂件,即打成压缩包在下载。
java下载过程其次别处的资源:可以是别的服务器,可以是⽹上的资源,当然也可以是本地的(更简单)
最后:⼀次性下载,⼀次性下载多个⽂件
三步⾛:
⼀、先将 “别处” 需要下载的⽂件下载到服务器,然后将⽂件的路径改掉
⼆、然后将服务器上的⽂件打成压缩包
三、下载这个压缩包
//下载
@RequestMapping("/download01")
public void downloadImage(String tcLwIds, HttpServletRequest request, HttpServletResponse response) throws Exception{ boolean dflag = false;
String[] paths = tcLwIds.split(",");
File [] file1 = new File[paths.length];
DownLoadImageUtil imageUtils = new DownLoadImageUtil();
if(paths.length > 1){
for (int i = 0; i < paths.length; i++) {
String imagepath=paths[i];
imageUtils.makeImage(imagepath); //将url的图⽚下载到本地,这个⽅法在下边
//修改为图⽚存放路径
file1[i] = new File("D:/upload/"+imagepath.substring(imagepath.lastIndexOf("/")));
}
filesDown(request, response, file1);//这是下边的⼀个⽅法
}
}
//将下载到服务器的图⽚放⼊压缩包
public void filesDown(HttpServletRequest request,HttpServletResponse response,File[] file1 ) throws Exception {
Random r=new Random();
String tmpFileName =r.nextInt(10000) +"downImage.zip";
String RealPath("/");
upath=upath.substring(0,upath.length()-1);
upath=upath.substring(0,upath.lastIndexOf("\\"));
//服务地址的存放路径
String FilePath = upath+"/ROOT/data/";
File f=new File(FilePath);
if(!f.exists()){ //路径不存在就创建
}
byte[] buffer = new byte[1024];
String strZipPath = FilePath + tmpFileName; //路径加⽂件名
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
for (int i = 0; i < file1.length; i++) {
FileInputStream fis = new FileInputStream(file1[i]);
torrentreactorout.putNextEntry(new ZipEntry(file1[i].getName()));
//设置压缩⽂件内的字符编码,不然会变成乱码
out.setEncoding("GBK");
int len;
// 读⼊需要下载的⽂件的内容,打包到zip⽂件
while ((len = ad(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
//下载的服务地址根据实际情况修改
boolean dflag = downloafile(request, response,"localhost:8080/data/"+tmpFileName);
//将服务器上压缩前的源⽂件删除
for (int i = 0; i < file1.length; i++) {
if (file1[i].isFile()) {
file1[i].delete();
}
}
//将服务器上的压缩包删除
File fileZip=new File(strZipPath);
fileZip.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
//写到本地
public boolean downloafile(HttpServletRequest request,HttpServletResponse response, String path) { String name = path.substring(path.lastIndexOf("/")+1);
String filename = deChineseDownloadFileName(request, name);
response.setHeader("Content-Disposition", "attachment; filename=" + filename + ";");
boolean flag = false;
try {
文本编辑器能删除重复URL url = new URL(path);
InputStream inStream = url.openConnection().getInputStream();
BufferedInputStream in = new BufferedInputStream(inStream);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
ServletOutputStream os = OutputStream();
os.ByteArray());
os.flush();
os.close();
flag = true;
} catch (Exception e) {
<("违法信息下载...出错了");
}
return flag;
}
makeImage(); ⽅法(如果是服务器上的图⽚,可以省略这⼀步,直接打包)
/**
* 下载图⽚,并按照指定的路径存储
* @param bean
* @param filePath
*/
public void makeImage( String filePath) {
// ⽹络请求所需变量建设英语construction
try {
//new⼀个URL对象
URL url = new URL(filePath);
//打开链接
oraclelike多个值HttpURLConnection conn = (HttpURLConnection)url.openConnection();
/
/设置请求⽅式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输⼊流获取图⽚数据
InputStream inStream = InputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建⼀个Buffer字符串
byte[] buffer = new byte[1024];execution速记
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
/
/使⽤⼀个输⼊流从buffer⾥把数据读取出来
while( (ad(buffer)) != -1 ){
//⽤输出流往buffer⾥写⼊数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
byte []ByteArray();
//先将图⽚从url下载到服务器的D:/upload/
File imageFile = new File("D:/upload/"+filePath.substring(filePath.lastIndexOf("/")));
//创建输出流
FileOutputStream foutStream = new FileOutputStream(imageFile);
foutStream.write(data);
/
/关闭输出流
foutStream.close();
inStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论