簡體   English   中英

使用REST或Web服務上載/下載文件

[英]Upload/Download File using REST or Web Services

是否可以使用REST或任何其他Web服務上傳/下載文件並發送HTML代碼?

必須使用:PHP,Java或ASP。

我認為會有所幫助。 至少在Java方面。 Actualy,看看整個教程

下面是一個使用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);
    }
}

是的......它可能。

但是,這取決於服務器端的實現。

但是,如果您只想要答案...。是的

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

是的可能,需要使用正確的mimetype來實現這一點。 如果你使用Rest,你可以在響應體中傳遞字符串...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM