A. java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
Java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
可以,壓縮只是一種演算法,什麼語言都可以,比如某種格式的文件中1001010(二進制)代表漢子的"中"字,那麼壓縮演算法就是在編碼不沖突的情況下可以改變編碼長度,比如壓縮之後中字變成1010,這樣就節省空間了,這是我隨便舉的例子,具體的對應演算法可以網上查
B. java伺服器如何對zip文件分包上傳
這個你是用什麼客戶端上傳呢?
java寫的客戶端和H5頁面都可以做這個操作,思路都是一樣的。
把文件切割再上傳,後台接受結束後再把文件合並。
在DB里做個記錄就是斷點續傳了嘛。
給點代碼提示:
js:
每次上傳2M,必須是支持H5的瀏覽器才行,兼容的問題需要注意!
functioncalculate(file,callBack){
varfileReader=newFileReader(),
blobSlice=File.prototype.mozSlice||File.prototype.webkitSlice||File.prototype.slice,
chunkSize=2097152,
//readinchunksof2MB
chunks=Math.ceil(file.size/chunkSize),
currentChunk=0,
spark=newSparkMD5();
fileReader.onload=function(e){
spark.appendBinary(e.target.result);//appendbinarystring
currentChunk++;
if(currentChunk<chunks){
loadNext();
}
else{
callBack(spark.end());
}
};
functionloadNext(){
varstart=currentChunk*chunkSize,
end=start+chunkSize>=file.size?file.size:start+chunkSize;
fileReader.readAsBinaryString(blobSlice.call(file,start,end));
};
loadNext();
}
java代碼沒什麼注釋,也不是全部的代碼看個大概意思,理解一下吧
根據文件的MD5碼來判斷每次上傳的文件是不是上傳過的。
如果是就找到上次的點告訴前台從哪開始上傳。
Messagemessage=newMessage();
PrintWriterout=response.getWriter();
=newServiceBreakpointUpload();
BreakpointShardshard=newBreakpointShard();
StringcurrentShardIndex="";
StringtotalShard="";
StringfileMD5="";
StringfileName="";
StringfileType="other";
try{
fileMD5=request.getParameter("fileMD5");
Partpart=request.getPart("fileData");
currentShardIndex=request.getParameter("currentShardIndex");
totalShard=request.getParameter("totalShard");
fileName=request.getParameter("fileName");
fileName=newString(fileName.getBytes("iso-8859-1"),"UTF-8");
fileType=request.getParameter("fileType");
StringtypeFolderName=service.getTypeFolder(fileType);
StringfolderPath=getServletContext().getRealPath("/upload/")+typeFolderName+File.separator;
Stringpath=folderPath+fileName+"-"+fileMD5+"-"+currentShardIndex;
System.out.println("fileName:"+fileName);
//是否初次上傳
if(!service.isUpload(fileMD5,fileType)){
BreakpointFilebreakpointFile=newBreakpointFile();
breakpointFile.setMd5(fileMD5);
breakpointFile.setFile_name(fileName);
breakpointFile.setTotal_shard(totalShard);
breakpointFile.setCurrent_shard_index(currentShardIndex);
breakpointFile.setFile_type(fileType);
breakpointFile.setPath(folderPath);
service.saveFile(breakpointFile);
}else{//返回上次完成位置
service.updateFile(fileMD5,currentShardIndex,fileType);
System.out.println("uploadshard"+currentShardIndex+"OK");
}
shard.setMd5(fileMD5);
shard.setShard_index(currentShardIndex);
shard.setPath(path);
service.saveShardFile(shard);
part.write(path);
if(currentShardIndex.equals(totalShard)){//上傳完成
System.out.println("");
service.mergeFiles(fileMD5,fileType);
System.out.println("mergeshardOK");
message.setData("completed");
}
message.setData(currentShardIndex);
out.println(JSONObject.fromObject(message).toString());
}catch(Exceptione){
e.printStackTrace();
message.setHasError(true);
message.setErrorMessage("錯誤!");
out.println(JSONObject.fromObject(message).toString());
}
}
C. java如何解壓頁面上傳到伺服器的zip文件
這個轉換肯定是會出錯的,struts 的formFile跟zipFile沒有直接關系,怎麼能這么強制轉化呢?
建議
1. 把文件保存到一個臨時目錄(保存為zip文件)
2. 讀取這個文件
3. 抽取想要的文件
4. 把臨時文件刪除
D. 怎樣用Java將文件追加到zip文件
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @project: Test
* @author chenssy
* @date 2013-7-28
* @Description: 文件壓縮工具類
* 將指定文件/文件夾壓縮成zip、rar壓縮文件
*/
public class CompressedFileUtil {
/**
* 默認構造函數
*/
public CompressedFileUtil(){
E. java將File壓縮成zip
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("d:\\test.zip"));
String test1="test1";
String test2="test2";
byte[] bytes1 = test1.getBytes("UTF-8");
byte[] bytes2 = test2.getBytes("UTF-8");
ZipEntry z1 = new ZipEntry("test1.txt");
zos.putNextEntry(z1);
zos.write(bytes1);
ZipEntry z2 = new ZipEntry("text2.txt");
zos.putNextEntry(z2);
zos.write(bytes2);
zos.closeEntry();
zos.close();
//流可以自己獲取
//java默認的包不支持中文(亂碼)
//使用apache的ZipOutputStream進行zip壓縮
是否可以解決您的問題?
F. java zip怎麼上傳到sftp上
使用SSH協議進行FTP傳輸的協議叫SFTP 換言之你的SSH協議一定啟用了,那麼使用基本linux命令在遠端執行即可。 我個人而言,JSCH一般是這樣用的:SFTP用於單純的文件上傳,之後直接使用基礎ssh協議執行遠端linux命令
G. java 如何把一個ZIP的壓縮包轉換String然後用json報文上傳,接收到json報文後再還原這個zip到指定目錄
轉BASE64吧,這樣的數據量略一一些
~~~~~~~~~
H. 如何使用java壓縮文件夾成為zip包(最簡單的
import java.io.File;

public class ZipCompressorByAnt {
private File zipFile;
/**
* 壓縮文件構造函數
* @param pathName 最終壓縮生成的壓縮文件:目錄+壓縮文件名.zip
*/
public ZipCompressorByAnt(String finalFile) {
zipFile = new File(finalFile);
}
/**
* 執行壓縮操作
* @param srcPathName 需要被壓縮的文件/文件夾
*/
public void compressExe(String srcPathName) {
System.out.println("srcPathName="+srcPathName);
File srcdir = new File(srcPathName);
if (!srcdir.exists()){
throw new RuntimeException(srcPathName + "不存在!");
}
Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夾 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); //排除哪些文件或文件夾
zip.addFileset(fileSet);
zip.execute();
}
}
public class TestZip {
public static void main(String[] args) {
ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\test1.zip ");
zca.compressExe("E:\test1");
}
}
/*如果 出現ant 的 52 51 50 等版本問題 可以去找對應的ant-1.8.2.jar 我開始用的ant-1.10.1.jar 就是這個包版本高了 一直報verson 52 版本問題*/
I. java程序 壓縮指定文件 格式(zip)
直接右鍵單擊文件夾(所有的文件存放在一個文件夾裡面)然後選擇壓縮 前提是你要有 zip壓縮程序