【问题标题】:how to get file content from file using gwtupload如何使用 gwtupload 从文件中获取文件内容
【发布时间】:2013-08-20 15:24:04
【问题描述】:

我正在使用 GWTUpload 上传文件。 我在 onFinishHandler 中获取服务器信息、文件名、内容类型等,但没有选项可以将文件内容从服务器获取到客户端。

注意:我正在尝试上传 XML 文件和 EXCEL 文件

我正在使用 GWT 2.4、GXT 3.0.1、GWTUpload 0.6.6

这是示例代码

客户端代码 - OnFinishHandler

u.addOnFinishUploadHandler(new OnFinishUploaderHandler() {
            @Override
            public void onFinish(IUploader uploader) {
                if (uploader.getStatus() == Status.SUCCESS) {

                    System.err.println(uploader.getServerResponse());

                    UploadedInfo info = uploader.getServerInfo();
                    System.out.println("File name " + info.name);
                    System.out.println("File content-type " + info.ctype);
                    System.out.println("File size " + info.size);

                    System.out.println("Server message " + info.message);
                }
            }
        });

Servlet 代码

public class GWTFileUploadServlet extends UploadAction {
    private static final long serialVersionUID = -6742854283091447922L;

    String fileContent;
    File uploadedFile;

    @Override
    public String executeAction(HttpServletRequest request,
            List<FileItem> sessionFiles) throws UploadActionException {
        String response = "";
        int cont = 0;
        for (FileItem item : sessionFiles) {
            if (false == item.isFormField()) {
                cont++;
                try {
                    File file = File.createTempFile("upload-", ".bin");
                    item.write(file);

                                        uploadedFile = file;
                    fileContent = item.getContentType();


                    response += "File saved as " + file.getAbsolutePath();

                } catch (Exception e) {
                    throw new UploadActionException(e.getMessage());
                }
            }
        }

        removeSessionFileItems(request);

        return response;
    }

    @Override
    public void getUploadedFile(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        if (fileContent != null && !fileContent.isEmpty()) {
            response.setContentType(fileContent);
            FileInputStream is = new FileInputStream(uploadedFile);
            copyFromInputStreamToOutputStream(is, response.getOutputStream());
        } else {
            renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);
        }
    }

}

请帮帮我....

【问题讨论】:

    标签: gwt gwtupload


    【解决方案1】:

    您可以在调用item.write(...) 时读取您在文件系统中创建的文件,但最好从收到的FileItem 中获得InputStream 并将其内容写入任何地方。例如,如果内容是 String,您可以使用 StringWritter 来获取它:

          InputStream inputStream = item.getInputStream();
    
          StringWriter writer = new StringWriter();
          IOUtils.copy(inputStream, writer);
          String theContentString = writer.toString();
    

    [编辑]

    要在客户端获取文件的内容,因此您必须使用任何方法从服务器检索它:

    • 如果文件内容为 ascii,则作为 gwtupload servlet 中的自定义消息:使用executeAction 的返回字符串。

    • 对 servlet 进行 RequestBuilder 调用,以使用上传器 url 值获取文件。

    • 使用任何 GWT ajax 机制,例如 RPC。

    在现代浏览器中,您可以在客户端获取文件的内容,而无需将其发送到服务器端。看看lib-gwt-file

    【讨论】:

    • GwtUpload 在内部使用 IOUtils.copy 将输入流复制到输出流。我的问题是如何获取要复制到输出流的文件内容。 copyFromInputStreamToOutputStream(is, response.getOutputStream());我想要 OnFinishHandler 中的响应
    • 我尝试使用 gwtupload 为 XML 工作,我将文件内容作为自定义消息获取..但是当我尝试上传 excel 文件时它不工作请帮助我
    • Excel 文件是二进制格式,因此您无法在自定义消息中返回其内容。您可以在服务器端使用 Base64Utils 将其转换为字符串,并使用我所说的任何方法返回客户端,然后您可以在客户端转换为二进制。我不明白的是为什么您需要在客户端中使用 excel 二进制文件,实际上您至少不能以传统方式处理其内容,您必须使用并非在所有浏览器中都可用的 html5 方法。
    • 我的要求是,用户将上传一个文件,我必须读取内容并将内容设置为 dto 中的 String 属性并将其发送到 webservices
    • 所以你首先需要一个服务器端的库来读取excel文件,你用它做什么?然后你要提取你感兴趣的文本,你知道行列吗?并将其保存在您的数据库中。最后,您可以将此文本信息作为字符串发送回客户端。
    【解决方案2】:

    在你的代码中你可以使用

    字节[] 文件; 文件 = item.get();

    您将在“文件”变量中以编码格式获取所有文件内容。

    【讨论】:

      猜你喜欢
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 2012-08-27
      • 1970-01-01
      • 2012-11-04
      • 2016-08-10
      相关资源
      最近更新 更多