今天弄flex上传图片到java,现在弄成功,中间也经常一点小波折,现记录一下。重点在java侧的实现。

flex侧:文件上载到在url参数中传递的URL。该URL必须是配置为接受上载的服务器脚本。Flash Player使用HTTP POST方法上载文件。处理上载的服务器脚本应收到包含下列元素的POST请求:

Content-Type,属于multipart/form-data

Content-Disposition,其name属性默认情况下设置为“Filedata”,filename属性设置为原始文件的名称

文件的二进制内容

java侧,用spring MVC实现,接收到图片,先存储到本地,然后上传到ftp,上代码:

 

 
  
  1. @Controller 
  2. public class FlashController extends BaseController{ 
  3.      
  4.     // 限制文件的上传大小   
  5.     private int maxPostSize = 100 * 1024 * 1024
  6.     public static String p_w_picpathPath = null
  7.     public static String p_w_picpathCdnPath = null
  8.      
  9.     @SuppressWarnings("unchecked"
  10.     @RequestMapping("/uploadImage"
  11.     @ResponseBody 
  12.     public String uploadImage(HttpServletRequest request, HttpServletResponse response) { 
  13.         try { 
  14.             if(p_w_picpathPath == null) {                  
  15.                 p_w_picpathPath = Config.getConfig("message.p_w_picpath.path"); 
  16.             } 
  17.             if(p_w_picpathCdnPath == null) { 
  18.                 p_w_picpathCdnPath = Config.getConfig("message.p_w_picpath.cdn.dir"); 
  19.             } 
  20.         } catch (IOException e) { 
  21.             // TODO Auto-generated catch block 
  22.             LoggerUtil.error("读取聊天图片的存储地址出错",e); 
  23.         }    
  24.         String productId = request.getParameter("productId"); 
  25.         if(StringUtils.isBlank(productId)) { 
  26.             productId = "default"
  27.         } 
  28.         LoggerUtil.debug("收到flash存储聊天图片的请求,productId:" + productId); 
  29.         LoggerUtil.outInInfo("收到flash存储聊天图片的请求,productId:" + productId); 
  30.          
  31.         // We use the FileUpload package provided by Apache to process the request. 
  32.         DiskFileItemFactory factory = new DiskFileItemFactory();   
  33.         factory.setSizeThreshold(4096);   
  34.         ServletFileUpload upload = new ServletFileUpload(factory);   
  35.         upload.setSizeMax(maxPostSize);  
  36.         String fileName = null
  37.         try {   
  38.             request.setCharacterEncoding("UTF-8");   
  39.             List fileItems = upload.parseRequest(request);   
  40.             Iterator iter = fileItems.iterator();   
  41.             while (iter.hasNext()) {   
  42.                 FileItem item = (FileItem) iter.next();   
  43.                 if (!item.isFormField()) {   
  44.                     fileName = item.getName();   
  45.                     //转换文件名  
  46.                     String fileType = fileName.substring(fileName.lastIndexOf(".")); 
  47.                     fileName = ImageUtils.getCreateImageFileName("","","",fileType); 
  48.                     item.write(new File(p_w_picpathPath + fileName));    
  49.                 }   
  50.             }   
  51.         } catch (FileUploadException e) {   
  52.             LoggerUtil.error("[在线客服]用户上传图片失败", e); 
  53.         } catch (UnsupportedEncodingException e) { 
  54.             // TODO Auto-generated catch block 
  55.               LoggerUtil.error("[在线客服]用户上传图片失败", e); 
  56.         } catch (IOException e) { 
  57.             // TODO Auto-generated catch block 
  58.               LoggerUtil.error("[在线客服]用户上传图片失败", e); 
  59.         } catch (Exception e) { 
  60.             // TODO Auto-generated catch block 
  61.               LoggerUtil.error("[在线客服]用户上传图片失败", e); 
  62.         }   
  63.  
  64.         //把图片上传到CDN 
  65.         FTPUtils ftpUtils = new FTPUtils("config"); 
  66.         try { 
  67.             boolean result = ftpUtils.connect(); 
  68.             if(!result) { 
  69.                 LoggerUtil.alarmInfo("连接FTP服务器失败"); 
  70.                 return "error"
  71.             } 
  72.         } catch (IOException e1) { 
  73.             // TODO Auto-generated catch block 
  74.             LoggerUtil.error("连接FTP服务器失败", e1); 
  75.             return "error"
  76.         } 
  77.          
  78.         String ftpName = p_w_picpathCdnPath + productId + "/" + DateUtil.formatDate(new Date(),"yyyyMM") + "/" + fileName; 
  79.         String srcImagePath = p_w_picpathPath+fileName; 
  80.         int uploadResult = Im4JavaUtils.uploadJPGImageByIm4Java(ftpUtils, srcImagePath, ftpName, ImageUtils.IMAGE_SIZE_TYPE_ORIG); 
  81.         LoggerUtil.debug("上传结果:" + uploadResult); 
  82.         if(uploadResult != FTPUtils.UPLOADSTATUS_UPLOAD_FILE_SUCESS) { 
  83.             return "error"
  84.         } 
  85.         String result = ftpUtils.getFtpHttpUrl()+ productId + "/" + DateUtil.formatDate(new Date(),"yyyyMM") + "/" + fileName; 
  86.         LoggerUtil.debug(result); 
  87.         return result; 
  88.     } 

附上两个很好的参考网址:

http://www.adobe.com/devnet/flex/articles/file_upload.html

http://blog.csdn.net/duanjingyu/article/details/5539690