Advertisement
Hakuhun

java ftp

Mar 20th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package DataBase;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9.  
  10. import javax.swing.JOptionPane;
  11.  
  12. import StreamBean.UIBean;
  13.  
  14. public class FTP{
  15.    
  16.     private String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
  17.     private final String user = "hakuhun", pass ="jelszo", host = "bakonyigi.heliohost.org";
  18.     private final int port = 21;
  19.     private final int BUFFER_SIZE = 4096;
  20.     private OutputStream os = null;
  21.     private FileInputStream fis = null;
  22.    
  23.        
  24.     public FTP(){
  25.        
  26.     }
  27.    
  28.     public void uploadVideo(File f){
  29.         ftpUrl = String.format(ftpUrl, user, pass, host, "public_ftp/videos/" + f.getName());
  30.         System.out.println("Upload URL: " + ftpUrl);
  31.        
  32.         try {
  33.            
  34.             URL url = new URL(ftpUrl);
  35.             URLConnection con = url.openConnection();
  36.             os = con.getOutputStream();
  37.             fis = new FileInputStream(f);
  38.            
  39.             byte[] buffer = new byte[BUFFER_SIZE];
  40.             int bytesRead = -1;
  41.               while ((bytesRead = fis.read(buffer)) != -1) {
  42.                   os.write(buffer, 0, bytesRead);
  43.               }        
  44.         } catch (Exception e) {
  45.             JOptionPane.showMessageDialog(null, e.getMessage());
  46.         }finally{
  47.             try {
  48.                 os.close();
  49.                 fis.close();
  50.             } catch (IOException e) {
  51.                 JOptionPane.showMessageDialog(null, e.getMessage());
  52.             }
  53.         }
  54.        
  55.     }
  56.    
  57.     public void uploadAvatar(File f){
  58.         File f2 = new UIBean().resizeAvatar(f);
  59.         ftpUrl = String.format(ftpUrl, user, pass, host, "public_ftp/avatars/" + f2.getName());
  60.         System.out.println("Upload URL: " + ftpUrl);    
  61.        
  62.         try {
  63.            
  64.             URL url = new URL(ftpUrl);
  65.             URLConnection con = url.openConnection();
  66.             os = con.getOutputStream();
  67.             fis = new FileInputStream(f);
  68.            
  69.             byte[] buffer = new byte[BUFFER_SIZE];
  70.             int bytesRead = -1;
  71.               while ((bytesRead = fis.read(buffer)) != -1) {
  72.                   os.write(buffer, 0, bytesRead);
  73.               }        
  74.         } catch (Exception e) {
  75.             JOptionPane.showMessageDialog(null, e.getMessage());
  76.         }finally{
  77.             try {
  78.                 os.close();
  79.                 fis.close();
  80.             } catch (IOException e) {
  81.                 JOptionPane.showMessageDialog(null, e.getMessage());
  82.             }
  83.             JOptionPane.showMessageDialog(null, "Sikeres feltöltés!");
  84.         }
  85.        
  86.     }
  87.    
  88.     public void uploadSubtitle(File f){
  89.         ftpUrl = String.format(ftpUrl, user, pass, host, "public_ftp/subs/" + f.getName());
  90.         System.out.println("Upload URL: " + ftpUrl);
  91.        
  92.         try {
  93.            
  94.             URL url = new URL(ftpUrl);
  95.             URLConnection con = url.openConnection();
  96.             os = con.getOutputStream();
  97.             fis = new FileInputStream(f);
  98.            
  99.             byte[] buffer = new byte[BUFFER_SIZE];
  100.             int bytesRead = -1;
  101.               while ((bytesRead = fis.read(buffer)) != -1) {
  102.                   os.write(buffer, 0, bytesRead);
  103.               }        
  104.         } catch (Exception e) {
  105.             JOptionPane.showMessageDialog(null, e.getMessage());
  106.         }finally{
  107.             try {
  108.                 os.close();
  109.                 fis.close();
  110.             } catch (IOException e) {
  111.                 JOptionPane.showMessageDialog(null, e.getMessage());
  112.             }
  113.         }
  114.        
  115.     }
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement