【问题标题】:Save a received picture to a folder on a web server将接收到的图片保存到 Web 服务器上的文件夹中
【发布时间】:2013-05-19 18:16:29
【问题描述】:

我要从安卓手机发送一张图片到我电脑上的本地网络服务器。我想将图片保存到本地服务器上的文件夹中。我的计划是编写某种控制器来处理接收到的图片并保存它。所以基本上我认为我需要创建一个控制器来接收参数(图片)并将其保存到服务器的文件夹中。我一直在寻找,但还没有找到我要找的东西。

因此我想知道的是:
这样的控制器是怎么写的。



我目前正在使用 Apache Tomcat/7.0.39 Web 服务器,通过 STS 使用 Spring MVC 框架,我的操作系统是 Windows 7。

感谢我能得到的任何帮助!
代码示例将不胜感激。

谢谢你,
垫子

【问题讨论】:

  • 我想图片最好作为 Base64 字符串发送。我听说过一些关于发布请求的信息,这可能会有所帮助吗?
  • 一般来说,您正在寻找的是“文件上传处理程序”。尝试搜索,如果您不必使用 Spring,有很多更简单的选择。

标签: java android spring spring-mvc webserver


【解决方案1】:

Apache Commons FileUpload 非常容易用于处理多部分表单帖子。我认为我没有将它与 Spring MVC 一起使用,但那里有 examples

【讨论】:

  • 是的,我刚刚发现它并设法编写了一个控制器来完成这项工作。
  • 还有不知道有没有什么好办法可以将安卓手机的图片发送到使用commons fileupload的服务器?`(代码基本怎么写)
  • 你的意思是来自Android浏览器上的网络表单,还是直接来自文件系统?如果是 html 表单,您只需在表单标签中指定 enctype="mulipart/form-data" 并提供 type="file" 的输入。如果它来自文件系统,则不需要 servlet 中的 FileUpload 库。您可以在 http 帖子中发送数据(查看 java.net.HttpURLConnection)。然后在 servlet 中,您只需读取二进制 ServletInputStream 并将数据缓冲到 FileOutputStream 中。
  • 是的,它是服务器端的 HTML 表单。它看起来像这样:<html> <head> <title>Upload a file please</title> </head> <body> <h1>Please upload a file</h1> <form method="post" action="/form" enctype="multipart/form-data"> <input type="text" name="name"/> <input type="file" name="file"/> <input type="submit"/> </form> </body> </html>
  • android 端的代码很长,无法在这里发布,但这些是(至少我认为)最重要的几行: String url = "localhost:8080/springmvc/upload";文件 file = new File(Environment.getExternalStorageDirectory(), "fileDir"); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = 新的 HttpPost(url); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); reqEntity.setContentType("binary/octet-stream"); reqEntity.setChunked(true); httppost.setEntity(reqEntity); HttpResponse 响应 = httpclient.execute(httppost);
【解决方案2】:

对于类似的功能(将照片从 Android 加载到 servlet),这是我使用的 Android 客户端代码(稍作编辑以便在此处发布):

URI uri = URI.create(// path to file);

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
// several key-value pairs to describe the data, one should be filename
entity.addPart("key", new StringBody("value"));

File inputFile = new File(photoUri.getPath());
// optionally reduces the size of the photo (you can replace with FileInputStream)
InputStream photoInput = getSizedPhotoInputStream(photoUri);
entity.addPart("CONTENT", new InputStreamBody(photoInput, inputFile.getName()));

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(uri); 
HttpContext localContext = new BasicHttpContext();

httppost.setEntity(entity);     
HttpResponse response = httpclient.execute(httppost, localContext);

这是接收它的代码。首先,确保将您的 servlet 类标记为支持多部分消息:

@MultipartConfig
public class PhotosServlet extends HttpServlet

然后是身体的相关部分:

HttpEntity entity = new InputStreamEntity(request.getPart("CONTENT").getInputStream(), contentLength);
InputStream inputFile = entity.getContent();

// string extension comes from one of the key-value pairs
String extension = request.getParameter(//filename key);

// first write file to a file
File images = new File(getServletContext().getRealPath("images"));
File filePath = File.createTempFile("user", extension, images);
writeInputDataToOutputFile(inputFile, filePath); // just copy input stream to output stream
String path = filePath.getPath();

logger.debug("Wrote new file, filename: " + path);

希望对你有帮助。

【讨论】:

  • 谢谢,我寻求了类似的解决方案!
【解决方案3】:

这是我使用 STS 和 MVC 框架模板的解决方案:

控制器:

@Controller
public class HomeController {@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
    byte[] bytes = file.getBytes();
    FileOutputStream fos = new FileOutputStream(
            "C:\\Users\\Mat\\Desktop\\image.bmp");
    try {
        fos.write(bytes);
    } finally {
        fos.close();
    }
    return "works";
} else {
    return "doesn't work";
}
}

}

.jsp 文件(表单):

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Upload a file please</title>
</head>
<body>
    <h1>Please upload a file</h1>
    <form method="post" action="/upload" enctype="multipart/form-data">
         <input type="file" name="file"/>
        <input type="submit"/>  
    </form>
</body>

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多