使用REST或Web服务上载/下载文件 - java

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

必须使用:PHP,Java或ASP。

参考方案

我认为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);
    }
}

Java Applet的URLConnection与PHP无效 - java

我已经研究了Oracle文档和示例,但仍然无法正常工作。我有一个Java Applet,它只是尝试使用URLConnection和OutputStreamWriter通过POST将文本字段发送到PHP脚本。 Java方面似乎工作正常,没有引发异常,但是PHP在我的页面上未显示任何输出。我是PHP新手,因此请耐心等待。这是相关的Java部分: try { UR…

菱形运算符<>是否等于<?> - java

我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…

Java中的<<或>>>是什么意思? - java

This question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…

<T>如何在这里处理String和Integer - java

我不明白T如何使用Integer和String。如此处显示功能中所示,T同时处理整数和字符串。该代码如何工作?class firstBase { <T> void display(T give_num, T give_String) { System.out.println("The given number is = " +…

SOAPFaultException部署在Tomcat上时,但在GlassFish中工作正常 - java

朋友们,我一直在尝试很多,阅读了很多论坛,但无法理解为什么出现此问题。我使用契约优先方法创建了一个Jax-WS WebService。创建WSDL和XSD,然后使用wsimport工具生成其余工件,为SEI提供实现。将WebService应用程序部署到Eclipse Helios中的GlassFish(Glassfish适配器和Eclipse中安装的插件)。…