【问题标题】:Process binary file in Java using FileItemStream使用 FileItemStream 在 Java 中处理二进制文件
【发布时间】:2012-03-11 15:41:03
【问题描述】:

我有一个允许上传二进制文件的网络应用程序。我必须解析它们并将内容 1:1 保存到字符串中,然后保存到数据库中。

当我在 unix 机器上使用 uuencode 对二进制文件进行编码时,它就可以工作了。有没有办法在 java 中自动执行此操作?

if (isMultipart) {

            //Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();

            //Parse the request
            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();

                InputStream stream = item.openStream();

                if (!item.isFormField()) {

                    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                    String line;
                    licenseString = "";

                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);

                        // Generate License File
                        licenseString += line + "\n";
                    }
                }
            }
            session.setAttribute("licenseFile", licenseString);
            System.out.println("adding licensestring to session. ");
        }

它当然适用于所有上传的非二进制文件。如何扩展它以支持二进制文件?

【问题讨论】:

    标签: java file-upload upload binary encode


    【解决方案1】:

    更好的方法是将上传内容写入临时文件,然后从那里处理:

    if (!item.isFormField()) {
    
        InputStream stream = new BufferedInputStream(item.getInputStream());
        BufferedOutputStream output = null;
    
        try {
            output = new BufferedOutputStream(new FileOutputStream(your_temp_file, false));
            int data = -1;
            while ((data = input.read()) != -1) {
                output.write(data);
            }
        } finally {
            input.close();
            output.close();
        }
    }
    

    现在您有了一个临时文件,与上传的文件相同,您可以从那里进行“其他”计算。

    【讨论】:

    • 谢谢,这是一个很好的输入,但我已经尝试过了。它不能解决我的“编码”问题:(
    【解决方案2】:

    您可以使用 commons_fileupload 库(在此处查看:org.apache.commons.fileupload.disk.DiskFileItem is not created properly?

    文档在这里:http://commons.apache.org/fileupload/using.html 你的案例在官网上解释得很好。

    【讨论】:

      【解决方案3】:
      // save to file
      // =======================================
      InputStream is = new BufferedInputStream(item.openStream());
      BufferedOutputStream output = null;
      
      try {
          output = new BufferedOutputStream(new FileOutputStream("temp.txt", false));
          int data = -1;
          while ((data = is.read()) != -1) {
              output.write(data);
          }
      } finally {
          is.close();
          output.close();
      }
      
      // read content of file
      // =======================================
      System.out.println("content of file:");
      try {
          FileInputStream fstream = new FileInputStream("temp.txt");
      
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String line;
      
          licenseString = "";
          String strLine;
          while ((strLine = br.readLine()) != null)   {
                System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()));
                licenseString += javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()) + "\n";
          }                                           
      } catch (Exception e) {
          System.err.println("Error: " + e.getMessage());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        相关资源
        最近更新 更多