spring ajax文件上传的问题 [英] problem with spring ajax file upload

查看:169
本文介绍了spring ajax文件上传的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单,可以选择上传图片,但上传文件但我没有使用此方法

I have a simple form with an option to upload image but to upload a file I am not using this method

<form:input path="logoData" id="image" type="file" />

而不是我使用 ajax upload jquery pulgin
问题是upload.parseRequest(request)在以下代码中返回null:

instead I am using ajax upload jquery pulgin. The problem is upload.parseRequest(request) is returning null in the below code :

@RequestMapping(value = "/upload.htm", method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request) throws FileUploadException{

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List<FileItem> items = upload.parseRequest(request);
     System.out.println("====ITEMS====" + items.size());


     System.out.println("----REQUEST---" +request.getParameter("uploadImg"));
        System.out.println("-----SIZE----" +request.getParameterMap().size());
        Map<String, String> map = request.getParameterMap();

        for(Map.Entry<String, String> entry : map.entrySet()){
            System.out.println("----KEY---" + entry.getKey() + "----value---" + entry.getValue());
        }


 // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    System.out.println("----IS MULTIPART---" +isMultipart);
    return "hello";
}

日志输出为:

==== ITEMS ==== 0
$
----请求--- null

----- SIZE ---- 0

---- IS MULTIPART --- true

====ITEMS====0
----REQUEST---null
-----SIZE----0
----IS MULTIPART---true

我的javascript代码是:

And my javascript code is :

new AjaxUpload('#upload', {
action : my_url+ 'methodName/upload.htm',
name : 'uploadImg',
autoSubmit : true,
responseType: 'html',
onChange: function(file, extension){   },   
onSubmit: function(file, extension) {

},
onComplete: function(file, html) {
    alert(file);
    alert(html);

}

});

IS MULTIPART显示为true但如何获取文件名以及如何存储。我尝试了一个没有ajax的例子,使用数据类型CommonsMultipartFile可以正常工作。
此外我在PHP中使用了ajaxupload,我得到的文件名为$ _FILES ['image'] ['name'],但我不知道java,因为我是java新手。
我在这个网站上关注了我的类似问题,但没有成功。

IS MULTIPART is showing true but how to get the file name and how to store it. I have tried an example without ajax and it works fine using datatype CommonsMultipartFile. Also I have used ajaxupload in PHP and I get the filename as $_FILES['image']['name'] but no idea in java as I am new to java. I have followed my similar question on this site but no success.

谢谢。

推荐答案

你可以缩短它。你需要一个多部分解析器:

You can make this shorter. You need a multipart-resolver:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

然后:

@Controller
public class FileUpoadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
    }

}

这篇关于spring ajax文件上传的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆