【问题标题】:howto read extract files in web server j2ee如何在 Web 服务器 j2ee 中读取提取文件
【发布时间】:2014-12-17 22:00:18
【问题描述】:

可以从 servlet 中的 .zip 文件解压缩并读取文件内容,而无需将其保存到硬盘驱动器。

在该方法中,我可以解压缩提取文件的名称,但是 ?????我想读取提取的文件

public void unZip2(HttpServletRequest req, HttpServletResponse resp, InputStream in)
        throws IOException {
    resp.setContentType("text/plain");

    PrintWriter out = resp.getWriter();

     try {

            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));

            ZipEntry entry;
            ZipEntry ze = null;

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


              ??????????????

            }

        } catch (Exception ex) {
         ex.printStackTrace();
            // throw new ServletException(ex);
        }
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    //unZip(request, response);

ServletFileUpload fileUpload = new ServletFileUpload();
// set the file size limit
fileUpload.setSizeMax(MAX_SIZE_LIMIT);

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

    try {
        FileItemIterator iterator = fileUpload.getItemIterator(request);
        out.println("dfsdfsdf"+iterator.hasNext());

        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream mathMLContent = item.openStream();

            if (item.isFormField()) {
                out.println("Got a form field: " + item.getFieldName());
            } else {
                String fileName = item.getName();
                String fieldName = item.getFieldName();
                String contentType = item.getContentType();

                InputStream oInputStream  = item.openStream();
                unZip2(request,response,oInputStream);
                out.println("fileName:" + fileName );
                out.println("fieldName:"+ fieldName );
                out.println("contentType:"+contentType);
            }
        }
    }catch(Exception e){

    }
}

【问题讨论】:

    标签: jakarta-ee servlets inputstream


    【解决方案1】:

    替换?????????????

    while ((entry = zis.getNextEntry()) != null) {
                                byte[] buf = new byte[1024];
                                int len;
                                while ((len = zis.read(buf)) > 0) {
                                    baos.write(buf, 0, len);
                                }
    
                                byte[] bytes = baos.toByteArray();      
    
                              String str = new String(bytes, "UTF-8");
                              System.out.println(">>>>>>>>>"+str);
                          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多