如何在Web服务器j2ee中读取提取文件

时间:2014-12-17 22:00:19

标签: java-ee servlets inputstream

可以从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){

    }
}

1 个答案:

答案 0 :(得分:0)

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

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);
                      }