如何使用Java上传zip文件?

编程入门 行业动态 更新时间:2024-10-26 02:30:49
本文介绍了如何使用Java上传zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图上传一个zip文件。在我的项目中,我在客户端使用DWR,在服务器端使用Java。正如我在DWR教程中看到的上传数据(它不在他们的网站上。他们提供dwr.rar包)他们通过以下行获得输入。

I trying to upload a zip file. In my project i am using DWR in the client side and Java in server side. As i have seen in DWR tutorials for uploading data(Its not in their website. They are providing it with dwr.rar bundle) they getting input by the below lines.

var image = dwr.util.getValue('uploadImage'); var file = dwr.util.getValue('uploadFile'); var color = dwr.util.getValue('color');

dwr.util.getValue()是一个获取值的实用程序任何元素,在本例中是一个文件对象。//在教程中提到。

dwr.util.getValue() is a utility to get the value of any element, in this case a file object.//Mentioned in the tutorial.

所以,我得到一个使用该文件的zip文件通过以下代码实用。

So, i get a zip file using that utility by the below code.

Javascript:

function uploadZip(){ var file = dwr.util.getValue("uploadFile"); dwr.util.setValue("uploadFile", null); DataUpload.uploadData(file, function(data){ if(data != null){ $("#zipURL").html("<p>Upload Completed!!!</p>"); $("#zipURL").append("Location: "+data.path2); } }); }

HTML:

<html> <head>ZIP Uploader </head> <body> <table> <tr><td>Select File: </td><td><input type="file" id="uploadFile" /></td> <tr><td><input type="button" value="Upload" onclick="uploadZip()" /></td></tr> </table> <div id="result"><span id="imgURL"></span> <span id="zipURL"></span></div> </body> </html>

Java代码是:

public class DataUpload { private static String DATA_STORE_LOC = "D:/BeenodData/Trials/"; public Path uploadData(InputStream file) throws IOException{//In the tutorial the //parameters are in type of BufferedImage & String. //They used it for image and text file respectively. //In an another example(out of DWR site) they used InputStream for receiving //image try { byte[] buffer = new byte[1024]; int c; File f2 = new File(DATA_STORE_LOC+dat+".zip"); path.setPath2(DATA_STORE_LOC+dat+".zip"); FileOutputStream fos = new FileOutputStream(f2); c = file.read(); System.out.println(c); while ((c = file.read()) != -1) { fos.write(c); } file.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return path; }

此代码运行无误。但输出是一个空的zip文件。我知道我做错了什么。我无法找到。

This code runs without error. But the output is a Empty zip file. I know i doing something wrong. I unable to find that.

实际上,我收到一个zip文件为 InputStream。

Actually, i am receiving a zip file as InputStream.

我应该如何使用java 将一个的InputStream(一个zip文件)写入zip.file ?

How should i have to write a InputStream(a zip file) to a zip.file using java?

如果我将java 方法参数设置为 ZipFile文件,会发生什么?我b $ b没有尝试过,但因为,我是仍然在寻找一个好的教程,以了解它。

What will happen if i set the java method parameter as ZipFile file? I didnt tried it, yet because, i am still searching a good tutorial to learn about it.

任何建议或链接都​​会更加赞赏!!!!! 先谢谢!!!

Any Suggestion or Links would be more appreciative!!!!! Thanks in Advance!!!

推荐答案

这里有2个关于创建ZIP文件的例子:

Here you have 2 examples about creating a ZIP file:

http://www.java2s。 com / Tutorial / Java / 0180_ File / 0601 _ZipOutputStream.htm

www.java2s/Tutorial/Java/0180_File/0601_ZipOutputStream.htm

以下是阅读ZIP文件的示例:

Here is an example about reading a ZIP file:

www.kodejava /examples/334.html

更多推荐

如何使用Java上传zip文件?

本文发布于:2023-11-26 03:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   上传   文件   Java   zip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!