import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class StreamCopy { private static final int BUFFER_SIZE = 1024; public static void main(final String[] args) throws Throwable { InputStream is = new FileInputStream("movie.mp4"); /* 245,084 Mo chez moi */ OutputStream os = new FileOutputStream("movie2.mp4"); copy(is, os, true); is = new FileInputStream("doc.odt"); /* 14 Ko chez moi */ os = new FileOutputStream("doc2.odt"); copy(is, os, true); } public static void copy(final InputStream is, final OutputStream os, boolean closeInput) throws Throwable { final BufferedInputStream bis = new BufferedInputStream(is); final BufferedOutputStream bos = new BufferedOutputStream(os); int i, size = 0; final byte buffer[] = new byte[BUFFER_SIZE]; while ((i = bis.available()) > 0) { size = i > BUFFER_SIZE ? BUFFER_SIZE : i; is.read(buffer, 0, size); bos.write(buffer, 0, size); } bos.close(); /* doit être fermé */ if(closeInput) bis.close(); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question/** * Cette méthode permet d'effectuer le transfert d'un fichier * @param in Le flux d'entrée du fichier * @param out Le flux de sortie du fichier * @param closeOnExit Indique s'il faut fermer les flux à la fin */ public static void transfert(InputStream in, OutputStream out, boolean closeOnExit, JProgressBar jbp, long length) { try { byte[] buffer = new byte[1024]; int n; int value = 0; long compteur = 0L; while( ( n = in.read(buffer) ) != -1 ) { out.write(buffer, 0, n); out.flush(); compteur += 1024; int newVal = (int) (compteur * 100 / length); if( newVal > value ) { value = newVal; jbp.setValue(value); } }//while( ( n = in.read(buffer) ) != -1 ) }//try catch (IOException ex) { JOptionPane.showMessageDialog(null, "Une erreur s'est produite pendant le transfert du fichier !\n" + ex.getMessage(), "Oups !", JOptionPane.WARNING_MESSAGE); }//catch (IOException ex) if( closeOnExit ) { try { in.close(); out.close(); }//try catch (IOException owned) {}//catch (IOException owned) }//if( closeOnExit ) }//public static void transfert(InputStream in, OutputStream out, boolean closeOnExit)
transfert(new FileInputStream(fichier), socket.getOutputStream(), true);
transfert(socket.getInputStream(), new FileOutputStream(fichier), true);
public static void transfert2(InputStream in, OutputStream out, boolean closeOnExit, JProgressBar jbp, long length) { final BufferedInputStream bis = new BufferedInputStream(in); final BufferedOutputStream bos = new BufferedOutputStream(out); int i, size = 0; final byte buffer[] = new byte[BUFFER_SIZE]; try { while( ( i = bis.available() ) > 0 ) { size = i > BUFFER_SIZE ? BUFFER_SIZE : i; bis.read(buffer, 0, size); bos.write(buffer, 0, size); } bos.close(); //Cela correspond à cette ligne if( closeOnExit ) { bis.close(); } } catch (IOException ex) { Logger.getLogger(SockeTools.class.getName()).log(Level.SEVERE, null, ex); } }
FileInputStream fis = new FileInputStream(this.socketon.getFile()); SockeTools.transfert2(fis, this.socket.getOutputStream(), true, this.socketon.getJPB(), lengths.get(i));