GwtUpload_GettingStarted

GwtUpload_GettingStarted  

Featured
Updated Dec 19, 2011 by manuel.carrasco.m

Usage of GWTUpload library.

  1. Download last version of the library: gwtupload-x.x.x.jar and include it in your classpath.
  2. Also, add these libraries to your application: commons-fileupload-1.2.jar, commons-io-1.3.1.jar and log4j.jar
  3. Edit your module file: Xxx.gwt.xml.
    <module>
    
      <!-- Include GWTUpload library -->
      <inherits name="gwtupload.GWTUpload"/>
      <!-- Load dinamically predefined styles in the library when the application starts -->
      <stylesheet src="Upload.css"/>
       
      <!-- Change this line with your project's entry-point -->
      <entry-point class="package.Xxx"/>
    </module>
  4. Edit your web.xml and include the default servlet. Be aware that the servlet provided depends on these libraries: commons-fileupload, commons-io and log4j, so include them in your package.
      <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>
  5. Or use your customized servlet (extending ActionUpload and overriding executeAction).
      <servlet>
        <servlet-name>uploadServlet</servlet-name>
        <servlet-class>my.package.MyCustomizedUploadServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>uploadServlet</servlet-name>
        <url-pattern>*.gupld</url-pattern>
      </servlet-mapping>
  6. Create your client application
     
    /**
     * 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);
        }
      };
    }
  7. Create your customize servlet. This is an example of how to save the received files in a temporary folder. Don't override neither doPost or doGet unless you know what you are doing:
    /**
     * 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 = "";
        int cont = 0;
        for (FileItem item : sessionFiles) {
          if (false == item.isFormField()) {
            cont ++;
            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();
        }
      }
    }
  8. This code is available as an Eclipse project with maven and ant scripts
  • Check out the project, import it in Eclipse an take a look to the code
      svn checkout http://gwtupload.googlecode.com/svn/trunk/GettingStarted
  1. There is a specific page for servlet stuff: Servlets
  2. Also you could use gwtupload's Sendmail example as a reference application:

IMPORTANT!!: How to ask questions

Please, send emails to the group gwtupload@googlegroups.com to ask for help.

The group is public and it is indexed, so anyone should find and read your questions/answers.

Note that Comments here will be ignored !!!.

Thank you.


已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 QueueForMcu 基于单片机实现的队列功能模块,主要用于8位、16位、32位非运行RTOS的单片机应用,兼容大多数单片机平台。 开源代码:https://.com/xiaoxinpro/QueueForMcu 一、特性 动态创建队列对象 动态设置队列数据缓冲区 静态指定队列元素数据长度 采用值传递的方式保存队列数据 二、快速使用 三、配置说明 目前QueueForMcu只有一个静态配置项,具体如下: 在文件 中有一个宏定义 用于指定队列元素的数据长度,默认是 ,可以根据需要更改为其他数据类型。 四、数据结构 队列的数据结构为 用于保存队列的状态,源码如下: 其中 为配置项中自定义的数据类型。 五、创建队列 1、创建队列缓存 由于我们采用值传递的方式保存队列数据,因此我们在创建队列前要手动创建一个队列缓存区,用于存放队列数据。 以上代码即创建一个大小为 的队列缓存区。 2、创建队列结构 接下来使用 创建队列结构,用于保存队列的状态: 3、初始化队列 准备好队列缓存和队列结构后调用 函数来创建队列,该函数原型如下: 参数说明: 参考代码: 六、压入队列 1、单数据压入 将数据压入队列尾部使用 函数,该函数原型如下: 参数说明: 返回值说明: 该函数会返回一个 枚举数据类型,返回值会根据队列状态返回以下几个值: 参考代码: 2、多数据压入 若需要将多个数据(数组)压入队列可以使用 函数,原理上循环调用 函数来实现的,函数原型如下: 参数说明: 当数组长度大于队列剩余长度时,数组多余的数据将被忽略。 返回值说明: 该函数将返回实际被压入到队列中的数据长度。 当队列中的剩余长度富余...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值