spring mvc 整合 uploadify 插件

第一步:uploadify v3.2 在前台的配置

 <input  name="file" class="input-xlarge" type="file" id="fileupload">

<button id="upload_file_btn" class="btn btn-primary">提交</button>

js配置:

$("#fileupload").uploadify({
        'auto':false,
        'fileObjName' : 'filedata',
        'swf': '../resources/exlib/uploadify/uploadify.swf',
        'uploader': './file',
        'fileTypeExts':'*.jpg;*.jpge;*.gif;*.png',

        'buttonText': '选择图片',
        'queueSizeLimit' : 1,
        //选择上传文件后调用
        'onSelect' : function(file) {
        },
        'onUploadStart' : function(file) {
        },
        'onCancel' : function(file) {
        },
        //上传到服务器,服务器返回相应信息到data里
        'onUploadSuccess':function(file, data, response){
        uploadFileSuccess(file, data, response);
        },
        'onUploadError':function(file, errorCode, errorMsg, errorString){
        uploadFileError(file, errorCode, errorMsg, errorString);
        }
    });

function uploadFile()
{

var formdata = 

{

    "name":$("#picname").val(),

    "link":$("#piclink").val(),

    "title":$("#pictitle").val(),

    "typeId":$("#pictype").val()

 };

$('#fileupload').uploadify('settings','formData',formdata);//动态设置formData


//start to upload

$('#fileupload').uploadify('upload','*');

}


第二步. MVC后台:

   刚开始想返回Map<String,String>,结果只有Chrome浏览器支持,IE9/FF都报了org.springframework.web.HttpMediaTypeNotAcceptableException错误,而且flash插件报I/O错误2038……只好返回String了。

    @RequestMapping(value="file",method=RequestMethod.POST)
    @ResponseBody
    public String upload(HttpServletRequest request,final HttpServletResponse response)
    {
        final MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        final ImgInfoBean imgInfoBean = new ImgInfoBean();
        final String name = (String) multipartRequest.getParameterValues("name")[0];
        final String typeId = (String) multipartRequest.getParameterValues("typeId")[0];
        final String link = (String) multipartRequest.getParameterValues("link")[0];
        final String title = (String) multipartRequest.getParameterValues("title")[0];


        imgInfoBean.setImgname(name);
        imgInfoBean.setImgtypeid(Integer.parseInt(typeId));
        imgInfoBean.setImgtitle(title);
        imgInfoBean.setImglink(link);


        final MultipartFile file = multipartRequest.getFile("filedata");


        final boolean flag = imgInfoServiceImpl.addImg(file, imgInfoBean, request, response);
        if(false == flag)
        {
            return new String("上传失败");
        }
        return new String("上传成功");
    }

编码转换配置

<mvc:annotation-driven >
        <!--确保@@ResponseBody 返回字符串是,格式是 text/plain;charset=UTF-8;
                                因为默认的是ISO-8859-1;-->

        <mvc:message-converters>   
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">   
          <property name="supportedMediaTypes">   
              <list>   
                  <value>text/plain;charset=UTF-8</value>   
                  <value>text/html;charset=UTF-8</value>   
              </list>   
          </property>   
      </bean>    
   </mvc:message-converters>   
</mvc:annotation-driven> 


自己的个人博客: bingtel-木犹如此的博客有兴趣可以关注下。


#########################

病友之家

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值