flexはjavaサーバに画像をアップロードします


今日flexをjavaにアップロードして、今成功して、真ん中もいつも少しの波乱があって、今記録します.java側の実装に重点を置きます.
flex側:urlパラメータに渡されたURLにファイルをアップロードします.このURLは、アップロードを受け入れるように構成されたサーバスクリプトである必要があります.Flash Playerは、HTTP POSTメソッドを使用してファイルをアップロードします.アップロードされたサーバ・スクリプトの処理は、次の要素を含むPOSTリクエストに送信されます.
Content-type、multipart/form-dataに属する
Content-Disposition、名前のプロパティはデフォルトで「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 imagePath = null
  7.     public static String imageCdnPath = null
  8.      
  9.     @SuppressWarnings("unchecked"
  10.     @RequestMapping("/uploadImage"
  11.     @ResponseBody 
  12.     public String uploadImage(HttpServletRequest request, HttpServletResponse response) { 
  13.         try { 
  14.             if(imagePath == null) {                  
  15.                 imagePath = Config.getConfig("message.image.path"); 
  16.             } 
  17.             if(imageCdnPath == null) { 
  18.                 imageCdnPath = Config.getConfig("message.image.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(imagePath + 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 = imageCdnPath + productId + "/" + DateUtil.formatDate(new Date(),"yyyyMM") + "/" + fileName; 
  79.         String srcImagePath = imagePath+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.     } 

2つの良い参考サイトを添付します.
http://www.adobe.com/devnet/flex/articles/file_upload.html
http://blog.csdn.net/duanjingyu/article/details/5539690