Upload/Download File using REST or Web Services

24,531

Solution 1

I think this will be helpful. At least when it comes to Java. Actualy, have a look at whole tutorial

Here is an example how to do it by using Spring:

Add commons-io and commons-fileupload dependencies to your pom.xml. Configure multipart resolver in your servlet context xml file:

<beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <beans:property name="maxUploadSize" value="10000000" />
</beans:bean>

This will be your JSP for upload of files (i.e. fileUpload.jsp):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
    <form:form method="post" commandName="upload" action="uploadNewFile"
        enctype="multipart/form-data">
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><label>File</label></td>
                    <td><input class="form-control" name="file" type="file"
                        id="file" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input class="btn btn-primary" type="submit"
                        value="Upload" /></td>
                </tr>
            </tbody>
        </table>
    </form:form>
</body>
</html>

And this is controller:

import java.io.IOException;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UploadController {

    // Show page for upload
    @RequestMapping(value = "/fileUpload", method = RequestMethod.GET)
    public ModelAndView showUploadFilePage() {
        return new ModelAndView("fileUpload", "upload", null);
    }

    // Get multipart file and save it
    @RequestMapping(value = "/uploadNewFile", method = RequestMethod.POST)
    public String save(@RequestParam("file") MultipartFile file) {
        // Save it to i.e. database
        // dao.save(file);
        return "fileUpload";
    }

    // Downloads file. I.e. JPG image
    @RequestMapping(value = "/download/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody HttpEntity<byte[]> getFile(
        @PathVariable("id") Integer id) throws IOException {

        byte[] file= dao.get(id).getImage();
        HttpHeaders header = new HttpHeaders();
        header.set("Content-Disposition", "attachment; filename=Image");
        header.setContentLength(file.length);

        return new HttpEntity<byte[]>(file, header);
    }
}

Solution 2

Yes...its possible.

It depends on the implementation on the server side though.

But if you just want an answer....its YES

http://blogs.msdn.com/b/uksharepoint/archive/2013/04/20/uploading-files-using-the-rest-api-and-client-side-techniques.aspx

Share:
24,531
Josias
Author by

Josias

Updated on July 02, 2020

Comments

  • Josias
    Josias almost 4 years

    Is it possible to Upload/Download a file using REST or any other Web Service and send HTML code?

    This has to be possible using: PHP, Java or ASP.

  • Josias
    Josias almost 11 years
    Thanks, this looks great and helful.
  • Branislav Lazic
    Branislav Lazic almost 11 years
    You are welcome. If it solves your problem, consider to accept answer. :)