js向java发送文件_将JSZip zip文件发送到Java

本文探讨了如何使用Angular 2前端通过JSZip上传ZIP文件,并将其发送到Java Spring Boot后端进行处理。问题在于如何正确发送和接收ZIP文件,以及如何在后端解析XML文件。尝试使用ZipInputStream处理接收到的字节数组未成功,当前无法获取ZIP条目的名称和内容。
摘要由CSDN通过智能技术生成

我的目标是将一个zip文件上传到我的Angular 2前端,并将其发送到我的Java(Spring Boot)后端并处理zip中包含的xml文件。

我正在使用JSZip将zip文件上传到我的Angular 2应用程序(但随意提出其他解决方案)。

问题是我不知道如何从前端发送zip文件,以及如何在后端接收它。

目前,我正在发送JSZip对象,如下所示:

{

"files":{

"file1.xml":{

"name":"file1.xml",

"dir":false,

"date":"2017-05-17T11:43:24.000Z",

"comment":null,

"unixPermissions":33188,

"dosPermissions":null,

"_data":{

"compressedSize":1408,

"uncompressedSize":7120,

"crc32":-1714370258,

"compression":{

"magic":"\b\u0000"

},

"compressedContent":{

"0":165,

"1":89,

"2":93,

"3":147,

(...)

"1348":54

}

},

"_dataBinary":true,

"options":{

"compression":null,

"compressionOptions":null

}

},

"file2.xml":{

(...)

}

},

"comment":null,

"root":""

}如何在Java中处理这些数据,以及如何处理xml文件?

基于this GitHub issue,

我试过这个:

@PostMapping(path = "zip")

public void importZip(@RequestBody final byte [] zipBytes) throws IOException {

final ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(zipBytes));

zipStream.getNextEntry();

ZipEntry entry;

while ((entry = zipStream.getNextEntry()) != null) {

System.out.println(entry.getName());

}

}但entry始终为null,并且不显示任何内容。

我该怎么做?

非常感谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单的利用javajs实现文件上传 package com.fendou.myString; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUpload extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean a=ServletFileUpload.isMultipartContent(request); if(a){ FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload (factory); Iterator items; try{ items=upload.parseRequest (request).iterator(); while(items.hasNext()){ FileItem item=(FileItem) items.next(); if(!item.isFormField()){ String name=item.getName (); String fileName=name.substring(name.lastIndexOf("\\")+1, name.length()); String path=request.getRealPath("file")+File.pathSeparatorChar+fileName; File uploadFile=new File (path); request.getSession ().setAttribute("file", uploadFile); item.write(uploadFile); response.setContentType ("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); // out.print("<font size='2'> 上传的文件为:"+name+"<br>"); // out.print("保存在服务器上 的地址为:"+path+"</font>"); } } }catch(Exception e){ e.printStackTrace(); } } response.sendRedirect("smartupload.jsp"); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值