2013-05-16 80 views
0

我要将android手机中的图片发送到我的计算机上的本地网络服务器。我想将图片保存到本地服务器上的文件夹中。我的计划是写一些控制器来照顾收到的图片并保存。所以基本上我认为我需要创建一个控制器来获取参数(图片)并将其保存到服务器的文件夹中。我一直在寻找,还没有找到我要找的东西。将收到的图片保存到网络服务器上的文件夹

所以我想知道的是:
这样的控制器是如何编写的。



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

Aprreciate任何帮助我能!
代码示例将不胜感激。

谢谢

+0

我想图片最好作为Base64字符串发送。我听说过有关发布请求的一些信息,这可能有帮助吗? – Mat

+0

一般来说,你要找的是一个“文件上传处理程序”。试着搜索一下,如果你不需要使用Spring,那么有很多更简单的选择。 –

回答

0

Apache Commons FileUpload是很容易使用来处理多形式的帖子。我不认为我已经使用它与Spring MVC,但有examples那里。

+0

是的,我刚刚发现它,并设法让一个控制器完成这项工作。 – Mat

+0

你也知道是否有从Android手机发送图片到使用commons fileupload的服务器的好方法吗?(基本上如何编写代码) – Mat

+0

您是指从Android浏览器上的Web表单或直接从文件系统?如果它是一个html表单,您只需在form标签中指定enctype =“mulipart/form-data”,并提供一个类型为“file”的输入。如果来自文件系统,则不需要servlet中的FileUpload库。你可以在http文章中发送数据(检出java.net.HttpURLConnection)。然后在servlet中,您只需读取二进制ServletInputStream并将数据缓冲到FileOutputStream中即可。 – Tap

0

对于类似的功能(从装载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); 

希望它能帮助。

+0

谢谢,我去了一个类似的解决方案! – Mat

0

这是我用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> 

相关问题