java 网络文件_java实现从网络下载多个文件
java從網(wǎng)絡(luò)下載多個文件,供大家參考,具體內(nèi)容如下
首先是打包下載多文件,即打成壓縮包在下載。
其次 別處的資源:可以是別的服務(wù)器,可以是網(wǎng)上的資源,當然也可以是本地的(更簡單)
最后:一次性下載,一次性下載多個文件
三步走:
一、先將 “別處” 需要下載的文件下載到服務(wù)器,然后將文件的路徑改掉
二、然后將服務(wù)器上的文件打成壓縮包
三、下載這個壓縮包
//下載
@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);//這是下邊的一個方法
}
}
//將下載到 服務(wù)器 的圖片 放入壓縮包
public void filesDown(HttpServletRequest request,HttpServletResponse response,File[] file1 ) throws Exception {
Random r=new Random();
String tmpFileName =r.nextInt(10000) +"downImage.zip";
String upath=request.getRealPath("/");
upath=upath.substring(0,upath.length()-1);
upath=upath.substring(0,upath.lastIndexOf("\\"));
//服務(wù)地址的存放路徑
String FilePath = upath+"/ROOT/data/";
File f=new File(FilePath);
if(!f.exists()){ //路徑不存在就創(chuàng)建
FileUtil.createDir(FilePath);
}
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]);
out.putNextEntry(new ZipEntry(file1[i].getName()));
//設(shè)置壓縮文件內(nèi)的字符編碼,不然會變成亂碼
out.setEncoding("GBK");
int len;
// 讀入需要下載的文件的內(nèi)容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
//下載的服務(wù)地址根據(jù)實際情況修改
boolean dflag = downloafile(request, response,"http://localhost:8080/data/"+tmpFileName);
//將服務(wù)器上 壓縮前的源文件 刪除
for (int i = 0; i < file1.length; i++) {
if (file1[i].isFile()) {
file1[i].delete();
}
}
//將服務(wù)器上的壓縮包刪除
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 = DownLoadImageUtil.encodeChineseDownloadFileName(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 = response.getOutputStream();
os.write(out.toByteArray());
os.flush();
os.close();
flag = true;
} catch (Exception e) {
logger.error("違法信息下載...出錯了");
}
return flag;
}
makeImage(); 方法(如果是服務(wù)器上的圖片,可以省略這一步,直接打包)
/**
* 下載圖片,并按照指定的路徑存儲
* @param bean
* @param filePath
*/
public void makeImage( String filePath) {
// 網(wǎng)絡(luò)請求所需變量
try {
//new一個URL對象
URL url = new URL(filePath);
//打開鏈接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設(shè)置請求方式為"GET"
conn.setRequestMethod("GET");
//超時響應(yīng)時間為5秒
conn.setConnectTimeout(5 * 1000);
//通過輸入流獲取圖片數(shù)據(jù)
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//創(chuàng)建一個Buffer字符串
byte[] buffer = new byte[1024];
//每次讀取的字符串長度,如果為-1,代表全部讀取完畢
int len = 0;
//使用一個輸入流從buffer里把數(shù)據(jù)讀取出來
while( (len=inStream.read(buffer)) != -1 ){
//用輸出流往buffer里寫入數(shù)據(jù),中間參數(shù)代表從哪個位置開始讀,len代表讀取的長度
outStream.write(buffer, 0, len);
}
byte []data=outStream.toByteArray();
//先將圖片從url下載到服務(wù)器的D:/upload/
File imageFile = new File("D:/upload/"+filePath.substring(filePath.lastIndexOf("/")));
//創(chuàng)建輸出流
FileOutputStream foutStream = new FileOutputStream(imageFile);
foutStream.write(data);
//關(guān)閉輸出流
foutStream.close();
inStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持谷谷點程序。
總結(jié)
以上是生活随笔為你收集整理的java 网络文件_java实现从网络下载多个文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【TL】【编码】瞬间移动-百度之星初赛(
- 下一篇: php七牛分片上传_ThinkPHP实现
