【问题标题】:Upload/Download File using REST or Web Services使用 REST 或 Web 服务上传/下载文件
【发布时间】:2013-06-17 22:30:10
【问题描述】:

是否可以使用 REST 或任何其他 Web 服务上传/下载文件并发送 HTML 代码?

这必须可以使用:PHP、Java 或 ASP。

【问题讨论】:

    标签: java php web-services rest asp-classic


    【解决方案1】:

    是的……有可能。

    这取决于服务器端的实现。

    但如果你只是想要一个答案......它是的

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

    【讨论】:

      【解决方案2】:

      我认为this 会有所帮助。至少在 Java 方面是这样。其实看看整个tutorial

      这是一个如何使用 Spring 的示例:

      将 commons-io 和 commons-fileupload 依赖项添加到您的 pom.xml。在您的 servlet 上下文 xml 文件中配置多部分解析器:

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

      这将是您上传文件的 JSP(即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>
      

      这是控制器:

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

      【讨论】:

      • 谢谢,这看起来很棒而且很有帮助。
      • 不客气。如果它解决了您的问题,请考虑接受答案。 :)
      【解决方案3】:

      是的,它可能,需要使用正确的 mimetype 来实现这一点。如果您使用 Rest...,则可以在响应正文中传递字符串...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 2014-07-17
        相关资源
        最近更新 更多