今天弄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