简体   繁体   English

如何使用springMVC Restful Service和jQuery上传文件

[英]how to upload a file with springMVC restful service and jQuery

I am developing an application with springMVC restful service as backend service and jQuery as front-end tech. 我正在开发一个使用springMVC Restful服务作为后端服务,使用jQuery作为前端技术的应用程序。

Now I have a requirement that files need to be uploaded by webservice to the server. 现在,我需要将文件通过Web服务上载到服务器。 I have no idea on how to write such a file uploading function. 我不知道如何编写这样的文件上传功能。 How should I send the file to the server by jQuery? 我应该如何通过jQuery将文件发送到服务器?

Thanks in advance. 提前致谢。

I'm not sure what you are asking for, this is what i got from you 我不确定您要的是什么,这是我从您那里得到的

controller code like this 像这样的控制器代码

map.put("a", "/Student_Photos/sample.jpg");

in jsp write your stuff and include this 在jsp中写你的东西,包括这个

<form:form modelAttribute="uploadItem" action="add" name="student"
                method="post" enctype="multipart/form-data"
                onSubmit="return validate();">

              <td width="152"  rowspan="4"  ><img id="blah"
                        src="<c:url value="${a}"/>" alt="your image" width="130"
                        height="110" />

                    <form:input path="fileData" id="image" type="file"
                            onchange="readURL(this)" /></td>
              </form:form>

There are two main ways to do it in the browser: 在浏览器中有两种主要方法:

  1. For modern browsers, use the HTML5 File API. 对于现代浏览器,请使用HTML5 File API。 See https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications 请参阅https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications

  2. If you need to support old browser, you POST the file to a hidden iFrame. 如果需要支持旧的浏览器,则将文件发布到隐藏的iFrame中。 See http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/ 参见http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/

On the server side, I am going to assume you are using Spring Data REST (SDR) for your REST servlet. 在服务器端,我将假设您正在对REST servlet使用Spring Data REST(SDR)。 SDR doesn't do file uploads, but you can run SDR along with a regular Spring MVC Dispatcher Servlet in the same servlet. SDR不会上传文件,但是您可以在同一Servlet中将SDR与常规的Spring MVC Dispatcher Servlet一起运行。

The handler method in your MVC Dispatcher's FileController would look something like this, if saving to database: 如果保存到数据库,则MVC Dispatcher的FileController中的处理程序方法将如下所示:

@RequestMapping(value = "/files", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
List<FileEntity> upload(@RequestParam("file") MultipartFile[] multipartFiles) throws Exception {

    List<FileEntity> response = new ArrayList<FileEntity>();

    for (MultipartFile multipartFile : multipartFiles) {

        FileEntity f = new FileEntity(multipartFile.getBytes(), multipartFile.getContentType(), multipartFile.getOriginalFilename());

        fileRepository.save(f);

        response.add(f);

    }

    return response;

}

You want SDR, Apache Commons FileUpload and IO on your classpath (or use the newer Servlet 3 API instead of Apache Commons). 您需要在类路径上使用SDR,Apache Commons FileUpload和IO(或使用更新的Servlet 3 API代替Apache Commons)。 Add them as maven dependencies: 将它们添加为Maven依赖项:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM