小编典典

Java 在GWT中上传基本文件

java

我试图弄清楚如何使用GWT的FileUpload小部件上传一个文件。我正在将GWT和Google AppEngine与Java一起使用,但是我想将文件上传到我自己的Linux服务器上。我已经有以下代码,但是现在我不知道如何将文件提交到Google AppServer服务器并将其保存到另一台服务器:

public class FileUploader{

    private ControlPanel cp;
    private FormPanel form = new FormPanel();
    private FileUpload fu =  new FileUpload();

    public FileUploader(ControlPanel cp) {
        this.cp = cp;
        this.cp.setPrimaryArea(getFileUploaderWidget());
    }

    @SuppressWarnings("deprecation")
    public Widget getFileUploaderWidget() {
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setMethod(FormPanel.METHOD_POST);
        // form.setAction(/* WHAT SHOULD I PUT HERE */);

        VerticalPanel holder = new VerticalPanel();

        fu.setName("upload");
        holder.add(fu);
        holder.add(new Button("Submit", new ClickHandler() {
            public void onClick(ClickEvent event) {
                GWT.log("You selected: " + fu.getFilename(), null);
                form.submit();
            }
        }));

        form.addSubmitHandler(new FormPanel.SubmitHandler() {
            public void onSubmit(SubmitEvent event) {
                if (!"".equalsIgnoreCase(fu.getFilename())) {
                    GWT.log("UPLOADING FILE????", null);
                                        // NOW WHAT????
                }
                else{
                    event.cancel(); // cancel the event
                }

            }
        });

        form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            public void onSubmitComplete(SubmitCompleteEvent event) {
                Window.alert(event.getResults());
            }
        });

        form.add(holder);

        return form;
    }
}

现在,我下一步需要做什么?我需要放入web.xml中的内容以及如何编写servlet,以便可以存储文件并返回该对象的url(如果可能)


阅读 416

收藏
2020-03-24

共1个答案

小编典典

这是我的应用程序中的代码:

1)我创建了一个接受HTTP请求的类:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.commons.fileupload.FileItemIterator; 
import org.apache.commons.fileupload.FileItemStream; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 

public class FileUpload extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        ServletFileUpload upload = new ServletFileUpload();

        try{
            FileItemIterator iter = upload.getItemIterator(request);

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

                String name = item.getFieldName();
                InputStream stream = item.openStream();


                // Process the input stream
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int len;
                byte[] buffer = new byte[8192];
                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, len);
                }

                int maxFileSize = 10*(1024*1024); //10 megs max 
                if (out.size() > maxFileSize) { 
                    throw new RuntimeException("File is > than " + maxFileSize);
                }
            }
        }
        catch(Exception e){
            throw new RuntimeException(e);
        }

    }
}

2)然后在我的web.xml中添加了以下几行:

<servlet>
    <servlet-name>fileUploaderServlet</servlet-name>
    <servlet-class>com.testapp.server.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>fileUploaderServlet</servlet-name>
  <url-pattern>/testapp/fileupload</url-pattern>
</servlet-mapping>

3)对于form.action这样做:

form.setAction(GWT.getModuleBaseURL()+"fileupload");
2020-03-24