Bild speichern Servlet

ThLu

Mitglied
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:

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.
 

ThLu

Mitglied
Es gab weder eine Exception, noch wurde das übermittelte Bild auf dem Server gespeichert. Habe mittlerweile eine funktionierende Lösung. Kann ich gerne bei Bedarf posten.

Thread kann also geschlossen werden :)

Viele Grüße,
Thomas.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
E Android Zeichnen auf Canvas - Teilbereich als Bild speichern Android & Cross-Platform Mobile Apps 3
W Bild aus dem Internet in View bzw. ImageView laden (Fragment) Android & Cross-Platform Mobile Apps 2
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
J Firebase Bild URL herausfinden Android & Cross-Platform Mobile Apps 0
M ImageButton: Bild ausblenden und Text anzeigen Android & Cross-Platform Mobile Apps 2
J Android ITextG Pdf Bild einfügen in PageEvents Android & Cross-Platform Mobile Apps 1
Alex/89 ImageView mit einem Bild von der SD Karte füllen Android & Cross-Platform Mobile Apps 5
J ImageView zeigt kein Bild an, Andere Elemente jedoch sichtbar Android & Cross-Platform Mobile Apps 3
D Java ME Bild vom Web-Service aus zurück geben Android & Cross-Platform Mobile Apps 8
J (Android) Bild(er) anzeigen lassen Android & Cross-Platform Mobile Apps 0
M Android Bild aus Galerie zuschneiden Android & Cross-Platform Mobile Apps 1
P Bild mit der Kamera abspeichern Android & Cross-Platform Mobile Apps 23
R Android Layout Bild mit Text Android & Cross-Platform Mobile Apps 13
G Android Button mit Bild Android & Cross-Platform Mobile Apps 4
P Android Button mit bild versehen Android & Cross-Platform Mobile Apps 4
J Schlicht und einfach Bild anzeigen ;) Android & Cross-Platform Mobile Apps 5
P Bild anzeigen Android & Cross-Platform Mobile Apps 3
W Foto machen und es direkt per URI speichern und auswählen Android & Cross-Platform Mobile Apps 4
M Firebase RealtimeDatabase - Daten aus Queries in CSV File speichern Android & Cross-Platform Mobile Apps 1
I Android CameraApp, eingebaute App nutzen und Ergebniss als Datei speichern Android & Cross-Platform Mobile Apps 0
R Android Daten speichern und anzeigen Android & Cross-Platform Mobile Apps 7
T Screenshot speichern ohne alte Datei überschreiben Android & Cross-Platform Mobile Apps 3
P Daten bzw Objekte auf das Handy lokal speichern Android & Cross-Platform Mobile Apps 29
P Objekt mit Shared preferences speichern Android & Cross-Platform Mobile Apps 0
J Download und speichern mit jsch klappt nicht Android & Cross-Platform Mobile Apps 5
L Android SQLite Datenbank auf SDCard speichern Android & Cross-Platform Mobile Apps 5
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
V Kennt jemand ein Programm , um .DEX Dateien zu öffnen, bearbeiten und wieder speichern? Android & Cross-Platform Mobile Apps 2
B Dateien Speichern Android & Cross-Platform Mobile Apps 6
G Android Daten in einer Datenbank speichern Android & Cross-Platform Mobile Apps 1
V Bytes (Wertebereich 0-255) über BLE empfangen,lesen und speichern Android & Cross-Platform Mobile Apps 1
JavaWolf165 Android Fehler beim Speichern/Downloaden einer Datei Android & Cross-Platform Mobile Apps 2
Anfänger2011 Speichern von bereits gefüllten Dateien Android & Cross-Platform Mobile Apps 8
M Android Speichern einer .txt Datei im InternalStorage Android & Cross-Platform Mobile Apps 2
B Fragen zum Speichern einer Datei auf dem Gerät? Android & Cross-Platform Mobile Apps 7
G Speichern jede zehnte sekunde Android & Cross-Platform Mobile Apps 1
G Sensordaten speichern Android & Cross-Platform Mobile Apps 3
S Dynamische EditText View eingaben in Datenbank speichern Android & Cross-Platform Mobile Apps 0
L Android Apache POI: Datei speichern geht nicht Android & Cross-Platform Mobile Apps 1
A Android Daten in der Cloud speichern (Google) Android & Cross-Platform Mobile Apps 1
Streeber Android ExpandableListView Child-Auswahl speichern Android & Cross-Platform Mobile Apps 3
S Android Geschossenes Foto in anderem Ordner speichern Android & Cross-Platform Mobile Apps 11
G Wissen frage - wo am besten Speichern. Android & Cross-Platform Mobile Apps 1
S Android Speichern von Informationen in Code oder extern Android & Cross-Platform Mobile Apps 0
L Android Text aus Textfeld speichern Android & Cross-Platform Mobile Apps 5
R Android Objekt in bestimmten Pfad auf Android-gerät speichern Android & Cross-Platform Mobile Apps 8
G Datei im Ordner speichern Android & Cross-Platform Mobile Apps 2
P Android Fragen reihenfolge speichern / laden Android & Cross-Platform Mobile Apps 2
B Inhalt von StringBuffer in Textdatei speichern? Android & Cross-Platform Mobile Apps 6
U Android Daten dauerhaft speichern? Android & Cross-Platform Mobile Apps 2
P Java ME benutzer daten speichern Android & Cross-Platform Mobile Apps 5
O eingehende Daten speichern Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben