I am to send a picture from an android phone to a local web server on my computer. I'd like to save the picture to a folder on the local server. My plan is to write some kind of controller that takes care of the received picture and saves it. So basically I think I need to create a controller that takes in a parameter (the picture) and saves it to a folder at the server. I have been searching all over and haven't yet found what I'm looking for.
Therefore what I'd like to know is:
How is such a controller written.
I am currently using Apache Tomcat/7.0.39 web server, Spring MVC Framework through STS and my OS is Windows 7.
Aprreciate any help I can get!
Code examples would be greatly appreciated.
Thank you,
Mat
Apache Commons FileUpload is pretty easy to use to process multipart form posts. I don't think I've used it with Spring MVC, but there are examples out there.
For a similar function (loading photos from Android to servlet), here's the Android client code I use (edited slightly for posting here):
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);
and here's the code to receive it. First, be sure to tag your servlet class as supporting multipart messages:
@MultipartConfig
public class PhotosServlet extends HttpServlet
and then the relevant part of the body:
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);
Hope it helps.
This is the solution I went with using STS with MVC Framework template:
The controller:
@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";
}
}
}
The .jsp file (the form):
<%@ 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.