生活随笔
收集整理的這篇文章主要介紹了
flex上传图片到java服务器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天弄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,上代碼:
?
@Controller?public?class?FlashController?extends?BaseController{???????????????private?int?maxPostSize?=?100?*?1024?*?1024;?????public?static?String?p_w_picpathPath?=?null;?????public?static?String?p_w_picpathCdnPath?=?null;??????????@SuppressWarnings("unchecked")?????@RequestMapping("/uploadImage")?????@ResponseBody?????public?String?uploadImage(HttpServletRequest?request,?HttpServletResponse?response)?{?????????try?{?????????????if(p_w_picpathPath?==?null)?{??????????????????????????????????p_w_picpathPath?=?Config.getConfig("message.p_w_picpath.path");?????????????}?????????????if(p_w_picpathCdnPath?==?null)?{?????????????????p_w_picpathCdnPath?=?Config.getConfig("message.p_w_picpath.cdn.dir");?????????????}?????????}?catch?(IOException?e)?{??????????????????????????LoggerUtil.error("讀取聊天圖片的存儲地址出錯",e);?????????}????????????String?productId?=?request.getParameter("productId");?????????if(StringUtils.isBlank(productId))?{?????????????productId?=?"default";?????????}?????????LoggerUtil.debug("收到flash存儲聊天圖片的請求,productId:"?+?productId);?????????LoggerUtil.outInInfo("收到flash存儲聊天圖片的請求,productId:"?+?productId);???????????????????????????DiskFileItemFactory?factory?=?new?DiskFileItemFactory();???????????factory.setSizeThreshold(4096);???????????ServletFileUpload?upload?=?new?ServletFileUpload(factory);???????????upload.setSizeMax(maxPostSize);??????????String?fileName?=?null;?????????try?{???????????????request.setCharacterEncoding("UTF-8");???????????????List?fileItems?=?upload.parseRequest(request);???????????????Iterator?iter?=?fileItems.iterator();???????????????while?(iter.hasNext())?{???????????????????FileItem?item?=?(FileItem)?iter.next();???????????????????if?(!item.isFormField())?{???????????????????????fileName?=?item.getName();????????????????????????????????????????????String?fileType?=?fileName.substring(fileName.lastIndexOf("."));?????????????????????fileName?=?ImageUtils.getCreateImageFileName("","","",fileType);?????????????????????item.write(new?File(p_w_picpathPath?+?fileName));????????????????????}???????????????}???????????}?catch?(FileUploadException?e)?{???????????????LoggerUtil.error("[在線客服]用戶上傳圖片失敗",?e);?????????}?catch?(UnsupportedEncodingException?e)?{????????????????????????????LoggerUtil.error("[在線客服]用戶上傳圖片失敗",?e);?????????}?catch?(IOException?e)?{????????????????????????????LoggerUtil.error("[在線客服]用戶上傳圖片失敗",?e);?????????}?catch?(Exception?e)?{????????????????????????????LoggerUtil.error("[在線客服]用戶上傳圖片失敗",?e);?????????}?????????????????????FTPUtils?ftpUtils?=?new?FTPUtils("config");?????????try?{?????????????boolean?result?=?ftpUtils.connect();?????????????if(!result)?{?????????????????LoggerUtil.alarmInfo("連接FTP服務器失敗");?????????????????return?"error";?????????????}?????????}?catch?(IOException?e1)?{??????????????????????????LoggerUtil.error("連接FTP服務器失敗",?e1);?????????????return?"error";?????????}??????????????????String?ftpName?=?p_w_picpathCdnPath?+?productId?+?"/"?+?DateUtil.formatDate(new?Date(),"yyyyMM")?+?"/"?+?fileName;?????????String?srcImagePath?=?p_w_picpathPath+fileName;?????????int?uploadResult?=?Im4JavaUtils.uploadJPGImageByIm4Java(ftpUtils,?srcImagePath,?ftpName,?ImageUtils.IMAGE_SIZE_TYPE_ORIG);?????????LoggerUtil.debug("上傳結果:"?+?uploadResult);?????????if(uploadResult?!=?FTPUtils.UPLOADSTATUS_UPLOAD_FILE_SUCESS)?{?????????????return?"error";?????????}?????????String?result?=?ftpUtils.getFtpHttpUrl()+?productId?+?"/"?+?DateUtil.formatDate(new?Date(),"yyyyMM")?+?"/"?+?fileName;?????????LoggerUtil.debug(result);?????????return?result;?????}?}? 附上兩個很好的參考網址:
http://www.adobe.com/devnet/flex/articles/file_upload.html
http://blog.csdn.net/duanjingyu/article/details/5539690
?
?
?
轉載于:https://blog.51cto.com/yamei/1034769
總結
以上是生活随笔為你收集整理的flex上传图片到java服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。