2014-06-16 1 views

Répondre

0

Ce Tutorial aidera les fichiers d'image de téléchargement et

Ce tutorial vous aidera à écrire des images à la base de données.

Ajout des parties de code:

Webservice pour le téléchargement de fichiers:

@Path("/file") 
public class UploadFileService { 

    @POST 
    @Path("/upload") 
    @Consumes(MediaType.MULTIPART_FORM_DATA) 
    public Response uploadFile(
     @FormDataParam("file") InputStream uploadedInputStream, 
     @FormDataParam("file") FormDataContentDisposition fileDetail) { 

     String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName(); 

     // save it 
     writeToFile(uploadedInputStream, uploadedFileLocation); 

     String output = "File uploaded to : " + uploadedFileLocation; 

      /************************************************/ 
      // CALL THE IMAGE UPLOAD TO DB CODE HERE. 
      // InsertImageTest.insertImage(); 
      /*************************************************/ 
     return Response.status(200).entity(output).build(); 

    } 

    // save uploaded file to new location 
    private void writeToFile(InputStream uploadedInputStream, 
     String uploadedFileLocation) { 

     try { 
      OutputStream out = new FileOutputStream(new File(
        uploadedFileLocation)); 
      int read = 0; 
      byte[] bytes = new byte[1024]; 

      out = new FileOutputStream(new File(uploadedFileLocation)); 
      while ((read = uploadedInputStream.read(bytes)) != -1) { 
       out.write(bytes, 0, read); 
      } 
      out.flush(); 
      out.close(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

    } 

} 

JAVA CODE POUR SAUVEGARDER D'IMAGE DB

public class InsertImageTest { 

    /** 
    * This is used to get the Connection 
    * 
    * @return 
    */ 
    public Connection getConnection() { 
     Connection connection = null; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/technicalkeeda", "root", ""); 
     } catch (Exception e) { 
      System.out.println("Error Occured While Getting the Connection: - " 
        + e); 
     } 
     return connection; 
    } 

    /** 
    * Insert Image 
    */ 
    public void insertImage() { 
     Connection connection = null; 
     PreparedStatement statement = null; 
     FileInputStream inputStream = null; 

     try { 
      File image = new File("C:/honda.jpg"); 
      inputStream = new FileInputStream(image); 
      connection = getConnection(); 
      statement = connection 
        .prepareStatement("insert into trn_imgs(img_title, img_data) " 
          + "values(?,?)"); 
      statement.setString(1, "Honda Car"); 
      statement.setBinaryStream(2, (InputStream) inputStream, 
        (int) (image.length())); 

      statement.executeUpdate(); 
     } catch (FileNotFoundException e) { 
      System.out.println("FileNotFoundException: - " + e); 
     } catch (SQLException e) { 
      System.out.println("SQLException: - " + e); 
     } finally { 
      try { 
       connection.close(); 
       statement.close(); 
      } catch (SQLException e) { 
       System.out.println("SQLException Finally: - " + e); 
      } 
     } 

    } 

    /*** 
    * Execute Program 
    * 
    * @param args 
    * @throws SQLException 
    */ 
    public static void main(String[] args) throws SQLException { 
     InsertImageTest imageTest = new InsertImageTest(); 
     imageTest.insertImage(); 
    } 

} 
0

La meilleure et facile télécharger n'importe quel fichier d'android à Mysql ser Ver est via l'utilisation de la bibliothèque client FTP. Link

Trouver le fichier ftp4j-1.6.jar et importer dans votre projet

vous pouvez télécharger l'image sur votre serveur via le code ci-dessous.

public void upload(){ 


    FTPClient con = null; 


     con = new FTPClient(); 
     con.connect("192.168.2.57"); // Your Server IP and Port you can use FTP domain credentials here also 

     if (con.login("XXXXXXXXX", "XXXXX")) // FTP username and Pass 
     { 
      con.enterLocalPassiveMode(); // important! 
      con.setFileType(FTP.BINARY_FILE_TYPE); 
      String data = "/sdcard/vivekm4a.m4a"; // Your File Path 

      FileInputStream in = new FileInputStream(new File(data)); 
      boolean result = con.storeFile("/vivekm4a.m4a", in); 
      in.close(); 
      if (result) Log.v("upload result", "succeeded"); 
      con.logout(); 
      con.disconnect(); 
     } 
    } 

} 

Hope this helps

1

Si vous souhaitez télécharger l'image par le service Web s'il vous plaît vérifier ci-dessous le code

public function changeprfoilePicture()  
{ 
     // $username='maik'; 
     $userid = $_POST['userid']; 
     $currentdate = date('Y-m-d H:i:s'); 
     $where=array('id'=>$userid); 
     $alias='id'; 
     $tabel='user'; 
     $userData =  $this->commonmodel->getData($alias,$tabel,$where); 
     if(empty($userData)) 
     { 
       echo "User not found"; 
       die; 
     } 
     $cur_timestamp = time(); 
      // profile thumb image 
     if(isset($_POST['v_thumb_image']) && trim($_POST['v_thumb_image']) !='') 
     { 
      if($userData != array()) 
      { 
       $thumb_img = $_POST['v_thumb_image']; 
       $thumb_img = str_replace('data:image/jpeg;base64,', '', $thumb_img); 
       $thumb_img = str_replace(' ', '+', $thumb_img); 
       $data = base64_decode($thumb_img); 
       $file_name = $userid."_".$cur_timestamp.'.jpg'; 
       $file = profile_thumb . $file_name; 
       $success = file_put_contents($file, $data); 
      } 
      else 
      { 
       echo $this->common->customFailMessage("User not found"); 
       die; 
      } 
     } 
      // Uplaod main image 
     if(isset($_POST['userfile']) && trim($_POST['userfile']) !='') 
      { 
        $mian_img = $_POST['userfile']; 
        $mian_img = str_replace('data:image/jpeg;base64,', '', $mian_img); 
        $mian_img = str_replace(' ', '+', $mian_img); 
        $data = base64_decode($mian_img); 
        $file_name = $userid."_".$cur_timestamp.'.jpg'; 
        $file = profile_main . $file_name; 
        $success = file_put_contents($file, $data); 
      } 

      if($success) 
       { 
        $valuesArray=array('update_time'=>"$currentdate",'profilepicture'=>"$file_name"); 
        $where= array('id'=>$userid); 
        $this->commonmodel->updateData('user',$valuesArray,$where); 
        $status='Success'; 
        $xml = new SimpleXMLElement('<Response/>'); 
        $xml->addChild('status', 'Success'); 
        $xml->addChild('profilepicture', $file_name); 
        print ($xml->asXML()); 
       } 
       else 
       { 
         $xml = new SimpleXMLElement('<Response/>'); 
         $xml->addChild('status', 'Fail'); 
         print ($xml->asXML()); 
       } 
    } 

Pour en savoir plus

click here

Questions connexes