Hallo zusammen,
ich möchte ein auf einem Android-Gerät gemachtes Bild via HttpServlet auf einem Server speichern. Allerdings passiert leider nichts.
Mein Code:
Android:
Übergeben wird die URL meines Servlets, der Dateipfad und der mime-type:
Mein Servlet (Im Internet gefunden und Copy/Paste):
Könnte mir da bitte jemand weiterhelfen?
Viele Grüße,
Thomas.
ich möchte ein auf einem Android-Gerät gemachtes Bild via HttpServlet auf einem Server speichern. Allerdings passiert leider nichts.
Mein Code:
Android:
Übergeben wird die URL meines Servlets, der Dateipfad und der mime-type:
Java:
public void saveFileTo(Uri uri, Uri fileUri, String mt){
try {
HttpPost post = new HttpPost(convertUri(uri));
File f = new File(fileUri.getPath());
FileEntity fileEntity = new FileEntity(f,mt);
post.setHeader("Content-Disposition",f.getName());
post.setEntity(fileEntity);
HttpResponse response = httpClient().execute(post);
}catch (IOException e) {
if (messageHandler!=null) messageHandler.onException(e);
} catch (URISyntaxException e) {
if (messageHandler!=null) messageHandler.onException(e);
}
}
Mein Servlet (Im Internet gefunden und Copy/Paste):
Code:
<servlet>
<description></description>
<display-name>PutFileServlet</display-name>
<servlet-name>PutFileServlet</servlet-name>
<servlet-class>fileServlets.PutFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PutFileServlet</servlet-name>
<url-pattern>/PUT/File</url-pattern>
</servlet-mapping>
<context-param>
<description>Location to store uploaded file</description>
<param-name>file-upload</param-name>
<param-value>
/var/opt/KWS
</param-value>
</context-param>
Java:
public class PutFileServlet extends HttpServlet {
private boolean isMultipart;
private String filePath;
private int maxFileSize = 50 * 1024;
private int maxMemSize = 4 * 1024;
private File file ;
public void init( ){
// Get the file location where it would be stored.
filePath = getServletContext().getInitParameter("file-upload");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
// Check that we have a file upload request
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("/var/opt/KWS"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
throw new ServletException("GET method used with " +getClass( ).getName( )+": POST method required.");
}
}
Könnte mir da bitte jemand weiterhelfen?
Viele Grüße,
Thomas.