今天弄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) {
- // TODO Auto-generated catch block
- LoggerUtil.error("读取聊天图片的存储地址出错",e);
- }
- String productId = request.getParameter("productId");
- if(StringUtils.isBlank(productId)) {
- productId = "default";
- }
- LoggerUtil.debug("收到flash存储聊天图片的请求,productId:" + productId);
- LoggerUtil.outInInfo("收到flash存储聊天图片的请求,productId:" + productId);
- // We use the FileUpload package provided by Apache to process the request.
- 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) {
- // TODO Auto-generated catch block
- LoggerUtil.error("[在线客服]用户上传图片失败", e);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- LoggerUtil.error("[在线客服]用户上传图片失败", e);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- LoggerUtil.error("[在线客服]用户上传图片失败", e);
- }
- //把图片上传到CDN
- FTPUtils ftpUtils = new FTPUtils("config");
- try {
- boolean result = ftpUtils.connect();
- if(!result) {
- LoggerUtil.alarmInfo("连接FTP服务器失败");
- return "error";
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- 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