GWT 批量上传

来源:互联网 发布:预测炒股票软件哪家好 编辑:程序博客网 时间:2024/06/07 00:59

 <context-param>    <!-- max size of the upload request -->    <param-name>maxSize</param-name>    <param-value>3145728</param-value>  </context-param>  <context-param>    <!-- Useful in development mode to slow down the uploads in fast networks.         Put the number of milliseconds to sleep in each block received in the server.         false or 0, means don't use slow uploads  -->    <param-name>slowUploads</param-name>    <param-value>200</param-value>  </context-param>  <servlet>    <servlet-name>uploadServlet</servlet-name>    <!-- This is the default servlet, it puts files in session -->    <servlet-class>gwtupload.server.UploadServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>uploadServlet</servlet-name>    <url-pattern>*.gupld</url-pattern>  </servlet-mapping>

















/** * An example of a MultiUploader panel using a very simple upload progress widget * The example also uses PreloadedImage to display uploaded images. *  * @author Manolo Carrasco Moñino */public class MultipleUploadSample implements EntryPoint {  // A panel where the thumbnails of uploaded images will be shown  private FlowPanel panelImages = new FlowPanel();  public void onModuleLoad() {    // Attach the image viewer to the document    RootPanel.get("thumbnails").add(panelImages);        // Create a new uploader panel and attach it to the document    MultiUploader defaultUploader = new MultiUploader();    RootPanel.get("default").add(defaultUploader);    // Add a finish handler which will load the image once the upload finishes    defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);  }  // Load the image in the document and in the case of success attach it to the viewer  private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {    public void onFinish(IUploader uploader) {      if (uploader.getStatus() == Status.SUCCESS) {        new PreloadedImage(uploader.fileUrl(), showImage);                // The server sends useful information to the client by default        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);        // You can send any customized message and parse it         System.out.println("Server message " + info.message);      }    }  };  // Attach an image to the pictures viewer  private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {    public void onLoad(PreloadedImage image) {      image.setWidth("75px");      panelImages.add(image);    }  };}




















/** * This is an example of how to use UploadAction class. *   * This servlet saves all received files in a temporary folder,  * and deletes them when the user sends a remove request. *  * @author Manolo Carrasco Moñino * */public class SampleUploadServlet extends UploadAction {  private static final long serialVersionUID = 1L;    Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();  /**   * Maintain a list with received files and their content types.    */  Hashtable<String, File> receivedFiles = new Hashtable<String, File>();  /**   * Override executeAction to save the received files in a custom place   * and delete this items from session.     */  @Override  public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {    String response = "";    for (FileItem item : sessionFiles) {      if (false == item.isFormField()) {        try {          /// Create a new file based on the remote file name in the client          // String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");          // File file =new File("/tmp/" + saveName);                    /// Create a temporary file placed in /tmp (only works in unix)          // File file = File.createTempFile("upload-", ".bin", new File("/tmp"));                    /// Create a temporary file placed in the default system temp folder          File file = File.createTempFile("upload-", ".bin");          item.write(file);                    /// Save a list with the received files          receivedFiles.put(item.getFieldName(), file);          receivedContentTypes.put(item.getFieldName(), item.getContentType());                    /// Send a customized message to the client.          response += "File saved as " + file.getAbsolutePath();        } catch (Exception e) {          throw new UploadActionException(e);        }      }    }        /// Remove files from session because we have a copy of them    removeSessionFileItems(request);        /// Send your customized message to the client.    return response;  }    /**   * Get the content of an uploaded file.   */  @Override  public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {    String fieldName = request.getParameter(UConsts.PARAM_SHOW);    File f = receivedFiles.get(fieldName);    if (f != null) {      response.setContentType(receivedContentTypes.get(fieldName));      FileInputStream is = new FileInputStream(f);      copyFromInputStreamToOutputStream(is, response.getOutputStream());    } else {      renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);   }  }    /**   * Remove a file when the user sends a delete request.   */  @Override  public void removeItem(HttpServletRequest request, String fieldName)  throws UploadActionException {    File file = receivedFiles.get(fieldName);    receivedFiles.remove(fieldName);    receivedContentTypes.remove(fieldName);    if (file != null) {      file.delete();    }  }}







































     final MultiUploader upload = new MultiUploader();
       
        upload.setServletPath(GWT.getModuleBaseURL()
                              + "uploadServlet");
    
        Label b = new Label();
        b.setWidth("116px");
        b.setStyleName("uploadFormButton");
        upload.addButton(b);


        add(upload,
            624,
            0);

0 0
原创粉丝点击