java flex 图片上传_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

publicclassFlashControllerextendsBaseController{

// 限制文件的上传大小

privateintmaxPostSize =100*1024*1024;

publicstaticString p_w_picpathPath =null;

publicstaticString p_w_picpathCdnPath =null;

@SuppressWarnings("unchecked")

@RequestMapping("/uploadImage")

@ResponseBody

publicString 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 = newDiskFileItemFactory();

factory.setSizeThreshold(4096);

ServletFileUpload upload = newServletFileUpload(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(newFile(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 = newFTPUtils("config");

try{

booleanresult = 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(newDate(),"yyyyMM") +"/"+ fileName;

String srcImagePath = p_w_picpathPath+fileName;

intuploadResult = 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(newDate(),"yyyyMM") +"/"+ fileName;

LoggerUtil.debug(result);

returnresult;

}

}

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值