Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java springajax文件上传问题_Java_Jquery_Spring Mvc_Jstl - Fatal编程技术网

Java springajax文件上传问题

Java springajax文件上传问题,java,jquery,spring-mvc,jstl,Java,Jquery,Spring Mvc,Jstl,我有一个简单的表单,可以选择上载图像,但要上载文件,我不使用这种方法 <form:input path="logoData" id="image" type="file" /> })) IS MULTIPART显示为true,但是如何获取文件名以及如何存储文件名。我尝试了一个没有ajax的示例,它使用数据类型CommonsMultipartFile运行良好。 另外,我在PHP中使用了ajaxupload,文件名为$_FILES['image']['name'],但在java中没有任

我有一个简单的表单,可以选择上载图像,但要上载文件,我不使用这种方法

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

IS MULTIPART显示为true,但是如何获取文件名以及如何存储文件名。我尝试了一个没有ajax的示例,它使用数据类型CommonsMultipartFile运行良好。 另外,我在PHP中使用了ajaxupload,文件名为$_FILES['image']['name'],但在java中没有任何概念,因为我对java是新手。 我在这个网站上也问过类似的问题,但没有成功


谢谢。

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

<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>

嘿,谢谢你的帮助…实际上我使用了相同的bean…但我认为@RequestParam只适用于GET请求。。!!请你给我完整的工作代码,因为我已经挣扎了两个多星期来完成这件事。嗨,波佐!你能告诉我如何用RestTemplate测试这个吗?非常感谢。我不记得了。你可以问一个单独的问题
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);

}
<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";
       }
    }

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

    if (!file.isEmpty()) {

            System.out.println("File name:"+ file.getOriginalFilename());
            //byte[] bytes = file.getBytes();
            System.out.println("Content type:"+ file.getContentType());
            String [] contentType = file.getContentType().split("/");
            String fileType = contentType[contentType.length-1];
            System.out.println("File type:"+ fileType);
            System.out.println("File size:"+ (file.getSize()/1024) + "KB");

            String path = context.getRealPath( "/resources/images/");
            System.out.println("Path: " + path);



            InputStream inputStream = null;
            OutputStream outputStream = null;
            try {
                inputStream = file.getInputStream();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                outputStream = new FileOutputStream(path + file.getOriginalFilename());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            int readBytes = 0;
            byte[] buffer = new byte[8192];
            try {
                while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
                            outputStream.write(buffer, 0, readBytes);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            // store the bytes somewhere
            return "theme";
    }
    else
        return "uploadError";

}