當前位置:首頁 » 編程語言 » java多文件下載

java多文件下載

發布時間: 2023-05-14 17:05:19

『壹』 高分:用java實現伺服器上多個文件先打包,然後下載,下載完成後刪除包!

壓縮包里添加文件時直接把伺服器上的文件用流讀進來就行,不用非把文件放到同一個目錄,用程序生成壓縮包和用命令行工具是不一樣的,不要想當然。 寫了個示常式序,你可以參考一下。這個示例不使用臨時文件,把 OutputStream os替換成你下載用的輸出流就可以實現一邊壓縮一邊下載。注意java.util.zip不支持非ascii文件名。想支持中文文件名可以用apache ant或其他的庫。

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipTest {

public static void main( String[] args ) {
try {
writeZip();
} catch ( IOException e ) {
e.printStackTrace();
}
}

private static void writeZip() throws IOException {
String[] files = { "/ws/dir1/file1", "/ws/dir2/file2", "/ws/file3", "/pub/success.wav" };
OutputStream os = new BufferedOutputStream( new FileOutputStream( "/ws/archive.zip" ) );
ZipOutputStream zos = new ZipOutputStream( os );
byte[] buf = new byte[8192];
int len;
for ( String filename : files ) {
File file = new File( filename );
if ( !file.isFile() ) continue;
ZipEntry ze = new ZipEntry( file.getName() );
zos.putNextEntry( ze );
BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
while ( ( len = bis.read( buf ) ) > 0 ) {
zos.write( buf, 0, len );
}
zos.closeEntry();
}
zos.close();
}

}

『貳』 java ftp連接一次下載多個文件

/**
*參考例子如下:
*@paramnamelist下載的文件列表
*@parampath下載路徑
*@paramzipname壓縮文件名稱
*/
publicvoidzipDownloadFile(HttpServletResponseresponse,List<string>namelist,Stringpath,Stringzipname){
byte[]buf=newbyte[1024];

try{

//本地保存設置
response.addHeader("Content-Disposition","attachment;filename="
+URLEncoder.encode(zipname,sysEncoding)+".zip");

response.setContentType("application/x-zip-compressed");

//向本地寫文件
ServletOutputStreamsos=response.getOutputStream();


ZipOutputStreamzipOut=newZipOutputStream(newBufferedOutputStream(sos));


for(Stringname:namelist){

ZipEntryentry=newZipEntry(name);
zipOut.putNextEntry(entry);

InputStreambis=this.getStream(path,name);
if(bis!=null){
intreadLen=-1;

while((readLen=bis.read(buf,0,1024))!=-1){
zipOut.write(buf,0,readLen);
}

bis.close();
}
}

zipOut.close();


}catch(Exceptione){

e.printStackTrace();
}

}</string>

『叄』 用java實現文件的下載,如何提高下載速度(非web開發)

下面貼出的代碼是一個簡單的讀取遠程文件保存到本地的實現,至於提高下載速度你可以利用多線程,具體可參考最下面的那個網址——

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class DownloadTester {
public static void main(String[] args) throws IOException {
String urlStr = "https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/logo-.gif";
String path = "D:/";

String name = urlStr.substring(urlStr.trim().lastIndexOf("/"));

URL url = new URL(urlStr);
InputStream in = url.openConnection().getInputStream();

File file = new File(path + name);
FileOutputStream out = new FileOutputStream(file, true);

int counter = 0;
int ch;
byte[] buffer = new byte[1024];
while ((ch = in.read(buffer)) != -1) {
out.write(buffer, 0, ch);
counter += ch;

System.out.println(counter + ":byte");
}
out.flush();
in.close();
out.close();
}
}

『肆』 用java下載指定路徑下的文件夾,下載內容包含指定文件夾及其包含的文件夾子文件!!

這個做不了的, 在計算機,你用命令去復制粘貼都需要指定是否遞歸復制
也就是說,如果你想下載指定的文件夾,你需要做很多的處理,一個一個文件的下載,然後下載到相對路徑中去,還有一種方案就是直接將文件夾打包再下載

『伍』 JAVA 如何一次下載多個文件

創建多線程下載
如果說方便下載,是打包再下載
~~~~~~~~~~~~~~~~~~~~~~

『陸』 Java 批量大文件上傳下載如何實現

解決這種大文件上傳不太可能用web上傳的方式,只有自己開發插件或是當門客戶端上傳,或者用現有的ftp等。
1)開發一個web插件。用於上傳文件。
2)開發一個FTP工具,不用web上傳。
3)用現有的FTP工具。
下面是幾款不錯的插件,你可以試試:
1)Jquery的uploadify插件。具體使用。你可以看幫助文檔。

『柒』 java下載多個文件瀏覽器彈出多個下載框

swing只會在伺服器中彈出多用於桌面程序如果用在web中則在頁面點擊時選擇框在伺服器彈出 java 導出文件彈出下載框讓用戶選悉悉擇路徑銷陸手 實現導虧嫌出文件...

『捌』 通過java實現文件下載

在jsp/servlet中斷點/多線程下載文件
<%@ page import="java.io.File" %><%@ page import="java.io.IOException" %><%@ page import="java.io.OutputStream" %><%@ page import="java.io.RandomAccessFile" %><%! public void downloadFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); java.io.FileInputStream fis = new java.io.FileInputStream(raf.getFD()); response.setHeader("Server", "www.trydone.com"); response.setHeader("Accept-Ranges", "bytes"); long pos = 0; long len; len = raf.length(); if (request.getHeader("Range") != null) { response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); pos = Long.parseLong(request.getHeader("Range") .replaceAll("bytes=", "") .replaceAll("-", "") ); } response.setHeader("Content-Length", Long.toString(len - pos)); if (pos != 0) { response.setHeader("Content-Range", new StringBuffer() .append("bytes ") .append(pos) .append("-") .append(Long.toString(len - 1)) .append("/") .append(len) .toString() ); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", new StringBuffer() .append("attachment;filename=\"") .append(file.getName()) .append("\"").toString()); raf.seek(pos); byte[] b = new byte[2048]; int i; OutputStream outs = response.getOutputStream(); while ((i = raf.read(b)) != -1) { outs.write(b, 0, i); } raf.close(); fis.close(); }%><% String filePath = request.getParameter("file"); filePath = application.getRealPath(filePath); File file = new File(filePath); downloadFile(request, response, file);%>
是否可以解決您的問題?

『玖』 JAVA文件下載如何實現

在http協議下,實現下載一般就兩種方法悄祥,一個採用cont-type="";此種方法為附件的方式下載;;
另一嘩運檔種較簡單,就是你只需要點下載按鈕然後跳轉到伺服器的那個文件路勁就可以了,瀏覽器自動亂亂回進行下載..

『拾』 java response.getOutputStream()實現多個文件下載,已經拿到兩個位元組數組的list,下載的時候如何同時下載

可以一個介面傳多個文件,每個文件中間用特定符號拆分,也可以寫一個介面前端多次調用,將請求頭的文件格式改為blob,前端獲取文件流後調用下載

熱點內容
pythonid3 發布:2025-07-17 13:31:50 瀏覽:325
文件被加密如何破解 發布:2025-07-17 13:31:50 瀏覽:29
網路編程經驗 發布:2025-07-17 13:13:20 瀏覽:67
學編程小孩 發布:2025-07-17 13:13:16 瀏覽:997
關電源能釋放緩存嗎 發布:2025-07-17 13:07:14 瀏覽:932
哪個moba配置要求較低 發布:2025-07-17 13:05:34 瀏覽:799
scratch編程視頻教學 發布:2025-07-17 12:50:36 瀏覽:636
linuxh264 發布:2025-07-17 12:20:12 瀏覽:481
主題密碼怎麼改 發布:2025-07-17 12:18:08 瀏覽:605
南京編譯中心招聘 發布:2025-07-17 12:18:08 瀏覽:962