導航:首頁 > 文件處理 > java下載壓縮文件

java下載壓縮文件

發布時間:2022-04-18 06:41:35

java 先根據一個list生成一個文件,然後在壓縮下載怎麼實現

編碼思路
(一)封裝list集合信息使之輸出到txt、excel等文件中;
(二)通過ZipOutputStream實現對文件壓縮操作;
(三)使用HttpServlet提供與用戶進行下載,下載完成後,刪除文件。

㈡ java完成批量下載時,壓縮文件怎麼命名

看你的代碼應該下載zip文件,對應的contentType 是application/x-zip-compressed

getResponse().setContentType("application/octet-stream");修改為getResponse().setContentType("application/x-zip-compressed");

㈢ java 下載異地FTP中的zip文件

這個要做定時任務的,ftp不可能主動給你發,只能自己每隔多長時間就去檢索一次,應該把ftp文件目錄結構和文件名稱全部存入資料庫,在下載時候對文件的標識狀態位進行更新,方便於對文件的判斷。然後從ftp下載文件即可。如果需要連接ftp下載文件的代碼,可以發送郵件到[email protected]

㈣ java 如何壓縮文件

樓主是說解壓了的文件大小隻有33.1MB,但是卻佔了51.2MB的空間嗎?
如果是這個意思的話,那我要告訴樓主,首先這個問題和JAVA沒有關系,根據你的截圖,可以斷定你用的是FAT32文件系統。這只是文件存儲的形式,很正常。
簡單的說,磁碟存儲數據的最小單位是簇,比方說你的簇大小為128K,一個簇只能存放一個文件,然後你的一個文件只有16K,它佔了這個簇,然後還有112K沒有用,是吧。但是那112K就不能再存放其它文件了。如果要存放其它文件,就要佔另一個簇。
樓主,懂了吧,這跟簇的大小有關,但是也不是簇越小越好,簇越小,讀寫性能都有所下降。這是正常現象。如果你非覺得不舒服,那就用NTFS文件系統吧,把壓縮打開,就不會有這種情況了,希望對你有幫助

㈤ java怎麼下載壓縮文件

可以用java的輸入,輸出流,設置返回的類型為下轉
response.setContentType("application/x-download");//設置為下載application/x-download
String filedownload = "/要下載的文件名";//即將下載的文件的相對路徑
String filedisplay = "最終要顯示給用戶的保存文件名";//下載文件時顯示的文件保存名稱
String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

㈥ 如何使用java壓縮文件夾成為zip包

在JDK中有一個zip工具類:

java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.

使用此類可以將文件夾或者多個文件進行打包壓縮操作。

在使用之前先了解關鍵方法:

ZipEntry(String name) Creates a new zip entry with the specified name.

使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipOutputStream;
/**
*將文件夾下面的文件
*打包成zip壓縮文件
*
*@authoradmin
*
*/
publicfinalclassFileToZip{

privateFileToZip(){}

/**
*將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下
*@paramsourceFilePath:待壓縮的文件路徑
*@paramzipFilePath:壓縮後存放路徑
*@paramfileName:壓縮後文件的名稱
*@return
*/
publicstaticbooleanfileToZip(StringsourceFilePath,StringzipFilePath,StringfileName){
booleanflag=false;
FilesourceFile=newFile(sourceFilePath);
FileInputStreamfis=null;
BufferedInputStreambis=null;
FileOutputStreamfos=null;
ZipOutputStreamzos=null;

if(sourceFile.exists()==false){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");
}else{
try{
FilezipFile=newFile(zipFilePath+"/"+fileName+".zip");
if(zipFile.exists()){
System.out.println(zipFilePath+"目錄下存在名字為:"+fileName+".zip"+"打包文件.");
}else{
File[]sourceFiles=sourceFile.listFiles();
if(null==sourceFiles||sourceFiles.length<1){
System.out.println("待壓縮的文件目錄:"+sourceFilePath+"裡面不存在文件,無需壓縮.");
}else{
fos=newFileOutputStream(zipFile);
zos=newZipOutputStream(newBufferedOutputStream(fos));
byte[]bufs=newbyte[1024*10];
for(inti=0;i<sourceFiles.length;i++){
//創建ZIP實體,並添加進壓縮包
ZipEntryzipEntry=newZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//讀取待壓縮的文件並寫進壓縮包里
fis=newFileInputStream(sourceFiles[i]);
bis=newBufferedInputStream(fis,1024*10);
intread=0;
while((read=bis.read(bufs,0,1024*10))!=-1){
zos.write(bufs,0,read);
}
}
flag=true;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}finally{
//關閉流
try{
if(null!=bis)bis.close();
if(null!=zos)zos.close();
}catch(IOExceptione){
e.printStackTrace();
thrownewRuntimeException(e);
}
}
}
returnflag;
}

publicstaticvoidmain(String[]args){
StringsourceFilePath="D:\TestFile";
StringzipFilePath="D:\tmp";
StringfileName="12700153file";
booleanflag=FileToZip.fileToZip(sourceFilePath,zipFilePath,fileName);
if(flag){
System.out.println("文件打包成功!");
}else{
System.out.println("文件打包失敗!");
}
}

}

㈦ 急!!!想實現通過java方式壓縮文件並提供下載功能,在線等!!!

給你一個代碼吧。
/**
* 文件夾壓縮。輸入路徑不能與輸出路徑相同
* zip("c:\\webserver\\test.zip","c:\\test1");
* @param zipFileName
* @param inputFile
* @throws Exception
*/
public static void zipFold(String zipFileName,String inputFilePath)throws Exception{
zip(zipFileName,new File(inputFilePath));
}

private static void zip(String zipFileName,File inputFile)throws Exception{
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
zipFold(out,inputFile,"");
out.close();
}

private static void zipFold(ZipOutputStream out,File f,String base)throws Exception{
if (f.isDirectory())
{
File[] fl=f.listFiles();
base=base.length()==0?"":base+"/";
for (int i=0;i<fl.length ;i++ )
{
zipFold(out,fl[i],base+fl[i].getName());
}
}
else
{
out.putNextEntry(new ZipEntry(base));
FileInputStream in=new FileInputStream(f);
int b;
while ((b=in.read()) != -1)
out.write(b);
in.close();
}
}

㈧ java 怎麼下載壓縮文件

伺服器生成壓縮文件,執行下載。

㈨ JAVA 批量下載.zip

/**
* 報表查詢模塊 ----文件下載流
* @return
* @throws IOException
*/
public InputStream getInputStream() throws IOException {
InputStream ins = new FileInputStream(zipReports());
return ins;
}

/**
* 根據傳過來的報表編號壓縮文件為zip
* @param response
* @param serverPath
* @param str
* @throws IOException
*/
public File zipReports() throws IOException{
List<StatisticalReport> srclist = new ArrayList<StatisticalReport>();
String[] pks = ids.split(",");
if(pks.length > 0){
for(String pk : pks){
String[] str = pk.split("\\|");
StatisticalReport obj = new StatisticalReport();
obj.setCendat(str[0]);
obj.setOrgidt(str[1]);
obj.setRep_code(str[2]);
obj.setCurcde(str[3]);
srclist.add(obj);
}
}
StatisticalReport obj = new StatisticalReport();
obj.setReportList(srclist);
//查詢要下載的報表文件
List<StatisticalReport> list = statisticalReportService.findReportList(obj);
//獲取應用在伺服器上的根目錄
String path = request.getSession().getServletContext().getRealPath(System.getProperty("file.separator"));
List<File> srcList = new ArrayList<File>();
if(list.size() > 0){
for(StatisticalReport statisticalReport : list){
File file = new File(statisticalReport.getFile_path());
if(file.exists()){
srcList.add(file);
}
}
}

Pim_sysUser user = (Pim_sysUser) session.getAttribute(SysConstant.SESSION_USER_DATA);
File zipfile = new File(path + System.getProperty("file.separator") + user.getLogid() + "REPORT.zip");
if(zipfile.exists()){
zipfile.delete();
zipfile.createNewFile();
}
//FileTools.File(, res.getString("help_path"), newFormatFileName);// 上傳文件
ZipUtils.zipFiles(srcList, zipfile);
return zipfile;
}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
/**
* 將多個Excel打包成zip文件
*
* @param srcfile
* @param zipfile
*/
public static void zipFiles(List<File> srcfile, File zipfile) {
byte[] buf = new byte[2048];
try {
// Create the ZIP file
// Compress the files
if(srcfile.size() > 0){
// 創建ZipOutputStream類對象
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < srcfile.size(); i++) {
File file = srcfile.get(i);
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.getName()));// 寫入此目錄的Entry 創建新的進入點
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.setLevel(9);
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
out.close();
}else{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
out.putNextEntry(new ZipEntry(" "));
out.closeEntry();
out.close();
}
// Complete the ZIP file
} catch (IOException e) {
e.printStackTrace();
}
}
}

㈩ 下載手機JAVA壓縮文件應該下載在手機哪個文件夾裡面

用不同的游覽器,所自動建立默認的文件夾也不同,那麼你上網下載的任何文件都會自動放入默認的文件夾里,可是你刪掉默認文件夾,它又會自己分類:如圖片會自動放入Images文件夾里,音樂會自動放入Sounds文件夾里,視頻會自動放入Videos文件夾里,還有其它的(主題,軟體,游戲等)就自動放入Others文件夾里,我個人建議把游覽器默認的文件夾刪掉好,這樣一來,下載下來的文件最基本有90%以上它幫你分類了,一來方便自己查想要找的文件,二來自己的手機的文件分配的整整有理。還有,我建議用UC6.1上網,根據我親自研究,6.1的比6.2以上的都要好,網速比較穩定,連接埠比較快,6.1唯一比它們差的就是畫面切換快,其實有時候追求最新的也不一定是最好的,我相信大家要的不是新的外表,而是追求完美的系統搭配和網上沖浪感覺!我試調試過UC6.1的網速,最高可以89KB/S,到92KB/S好少能到,我手機現在最少都可以43.幾KB/S。而UC6.2就最高36.幾KB/S,我已經調到封頂了。如果那位玩機的朋友,想了解更多手機S60V2/S60V3系統,軟體的。我的回答完了,謝謝!

閱讀全文

與java下載壓縮文件相關的資料

熱點內容
什麼app零粉分發視頻有收益 瀏覽:162
肯亞程序員 瀏覽:638
新科源碼 瀏覽:659
如何判斷伺服器有沒有帶寬 瀏覽:41
天正建築批量刪除命令 瀏覽:94
cad最下面的一排命令都什麼意思 瀏覽:456
pythonimportcpp 瀏覽:850
W10的系統怎麼給U盤加密 瀏覽:370
華為手機代碼編程教學入門 瀏覽:762
和彩雲沒會員怎樣解壓 瀏覽:634
androidimageview保存 瀏覽:387
新買店鋪什麼伺服器 瀏覽:883
文件夾能直接刻錄嗎 瀏覽:493
androidxmpp刪除好友 瀏覽:969
javac哪個前景好 瀏覽:428
中華英才網app為什麼不能搜索了 瀏覽:660
伺服器域名是什麼意思 瀏覽:52
Linux導出mysql命令 瀏覽:159
無詐建鄴是什麼app 瀏覽:229
python中的雙色球 瀏覽:168