当前位置:首页 > 开发 > Web前端 > 前端 > 正文

Flex4: How to Upload File to Servlet (Part II)

发表于: 2013-04-23   作者:DavyJones2010   来源:转载   浏览次数:
摘要: 1. In this part, we focus on server side programming:     1) At what format did the data sent from Flex?     2) How did Servlet get data sent from Flex?     3) How did

1. In this part, we focus on server side programming:

    1) At what format did the data sent from Flex?

    2) How did Servlet get data sent from Flex?

    3) How did Servlet send response to Flex to notify upload finished?

    4) How to make it easier for common file upload?

 

2. Use commons-io-2.4.jar and commons-fileupload-1.3.jar for easier file upload

 

package edu.xmu.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet
{
	private int maxPostSize = 1024 * 1024 * 1024;
	private String uploadPath = "E:/SampleUploadFolder/";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.out.println("doGet");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.out.println("doPost");
		req.setCharacterEncoding("UTF-8"); // If we don't add this, garbled may occur with Chinese
		resp.setContentType("text/html;charset=UTF-8");
		// Set the minimal file size
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 2);
		// Set the max file size
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(maxPostSize);

		List<FileItem> fileItems = null;
		try
		{
			fileItems = upload.parseRequest(req);
			Iterator<FileItem> iter = fileItems.iterator();
			while (iter.hasNext())
			{
				FileItem fileItem = iter.next();
				if (!fileItem.isFormField())
				{
					String fileName = fileItem.getName();
					System.out.println("fileName = " + fileName);
					fileItem.write(new File(uploadPath, fileName));
				}
			}

		} catch (FileUploadException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

 

3. Actually the server don't have to send message to Flex explicitly. In Flex fileReference will monitor on file upload process.

 

Comments:

    1)May have java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException 

       A: Don't use UserLibrary. Copy commons-io-2.4.jar and commons-fileupload-1.3.jar into WebRoot/WEB-INF/lib folder instead. 

Flex4: How to Upload File to Servlet (Part II)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1. Flex <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.ad
/** * servlet 代码 */ public class UploadServlet extends HttpServlet { public void service(Ht
/** * Servlet 代码 */ import java.io.File; import javax.servlet.ServletContext; import org.ap
[原文链接:http://engineering.richrelevance.com/recommendations-thompson-sampling/。] [本文链
在实际开发中,我们经常需要文件上传功能。一个很棒的例子是BlueImpd的File Upload控件(https://gi
在实际开发中,我们经常需要文件上传功能。一个很棒的例子是BlueImpd的File Upload控件(https://gi
方法一 模板加载 首先把静态图像放在 polls/static/polls/images/background.png 其次把css文件放在
Previous Article New Features of WCF 4.0: Part I Introduction Microsoft.NET 4.0 and Visual St
Part-I of this article covered cryptography in the context of encryption. This part continues
Playing with ptrace, Part II by Pradeep Padala p_padala@yahoo.com http://www.cise.ufl.edu/~pp
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号