「java上传zip」java上传zip文件到服务器并解压
本篇文章给大家谈谈java上传zip,以及java上传zip文件到服务器并解压对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java如何解压页面上传到服务器的zip文件
直接通过工具类进行解压或者压缩文件即可。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;
/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解压Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解压Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration? extends ZipEntry entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}
/**
* 将输入流中的数据写到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}
/**
* 安全关闭多个流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
java怎么把.zip文件上传到另外的文件服务器
这个转换肯定是会出错的,struts 的formFile跟zipFile没有直接关系,怎么能这么强制转化呢?
建议
1. 把文件保存到一个临时目录(保存为zip文件)
2. 读取这个文件
3. 抽取想要的文件
4. 把临时文件删除
java服务器如何对zip文件分包上传?
这个你是用什么客户端上传呢?
java写的客户端和H5页面都可以做这个操作,思路都是一样的。
把文件切割再上传,后台接受结束后再把文件合并。
在DB里做个记录就是断点续传了嘛。
给点代码提示:
js:
每次上传2M,必须是支持H5的浏览器才行,兼容的问题需要注意!
function calculate(file,callBack){
var fileReader = new FileReader(),
blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice,
chunkSize = 2097152,
// read in chunks of 2MB
chunks = Math.ceil(file.size / chunkSize),
currentChunk = 0,
spark = new SparkMD5();
fileReader.onload = function(e) {
spark.appendBinary(e.target.result); // append binary string
currentChunk++;
if (currentChunk chunks) {
loadNext();
}
else {
callBack(spark.end());
}
};
function loadNext() {
var start = currentChunk * chunkSize,
end = start + chunkSize = file.size ? file.size : start + chunkSize;
fileReader.readAsBinaryString(blobSlice.call(file, start, end));
};
loadNext();
}
java代码没什么注释,也不是全部的代码看个大概意思,理解一下吧
根据文件的MD5码来判断每次上传的文件是不是上传过的。
如果是就找到上次的点告诉前台从哪开始上传。
Message message = new Message();
PrintWriter out = response.getWriter();
ServiceBreakpointUpload service = new ServiceBreakpointUpload();
BreakpointShard shard = new BreakpointShard();
String currentShardIndex = "";
String totalShard = "";
String fileMD5 = "";
String fileName = "";
String fileType = "other";
try {
fileMD5 = request.getParameter("fileMD5");
Part part = request.getPart("fileData");
currentShardIndex = request.getParameter("currentShardIndex");
totalShard = request.getParameter("totalShard");
fileName = request.getParameter("fileName");
fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");
fileType = request.getParameter("fileType");
String typeFolderName = service.getTypeFolder(fileType);
String folderPath = getServletContext().getRealPath("/upload/") + typeFolderName + File.separator;
String path = folderPath + fileName + "-" + fileMD5 + "-" + currentShardIndex;
System.out.println("fileName:"+fileName);
// 是否初次上传
if (!service.isUpload(fileMD5,fileType)) {
BreakpointFile breakpointFile = new BreakpointFile();
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("upload shard "+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("upload File finsh start merge shard");
service.mergeFiles(fileMD5,fileType);
System.out.println("merge shard OK");
message.setData("completed");
}
message.setData(currentShardIndex);
out.println(JSONObject.fromObject(message).toString());
} catch (Exception e) {
e.printStackTrace();
message.setHasError(true);
message.setErrorMessage("错误!");
out.println(JSONObject.fromObject(message).toString());
}
}
java上传zip的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java上传zip文件到服务器并解压、java上传zip的信息别忘了在本站进行查找喔。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。