今天弄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?imagePath?=?null;?
- ????public?static?String?imageCdnPath?=?null;?
- ?????
- ????@SuppressWarnings("unchecked")?
- ????@RequestMapping("/uploadImage")?
- ????@ResponseBody?
- ????public?String?uploadImage(HttpServletRequest?request,?HttpServletResponse?response)?{?
- ????????try?{?
- ????????????if(imagePath?==?null)?{??????????????????
- ????????????????imagePath?=?Config.getConfig("message.image.path");?
- ????????????}?
- ????????????if(imageCdnPath?==?null)?{?
- ????????????????imageCdnPath?=?Config.getConfig("message.image.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(imagePath?+?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?=?imageCdnPath?+?productId?+?"/"?+?DateUtil.formatDate(new?Date(),"yyyyMM")?+?"/"?+?fileName;?
- ????????String?srcImagePath?=?imagePath+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