Bonjour,

J'aimerai créer à partir d'un fichier , un tableau de byte afin de l'enregistrer sous forme de BLOB dans ma base de données.

De plus j'aimerai avoir une fonction inverse pour recréer le fichier à partir d'un tableau de bytes.

Comment dois-je procéder ??


J'ai ces 2 méthodes mais elles ne fonctionne que sur les fichier <64Ko

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public void TableauDeBytesVersFichier (byte[] tableau, String fic) throws java.io.IOException {
		try {
		    java.io.FileWriter f = new java.io.FileWriter(fic);
		    for (int i = 0; i < tableau.length; i++) {
   			    f.write((int)tableau[i]);
	    	}	 
			f.close();
		}
		catch(IOException e){
			System.out.println(e+"erreur lors de la lecture du fichier");
		}
 
	}
 
 
 
	/**
         * Permet de copier le contenu d'un fichier dans un tableau byte[]
         **/ 
	public byte[] FichierVersTableauDeBytes (String fic) throws java.io.IOException {
		java.io.File fichier = new File(fic);
		int i=0;
		int ch;
		byte[] tableau;
		try {			
			FileInputStream f= new FileInputStream(fichier);
			try{
				tableau = new byte[(int)fichier.length()];				
				while((ch=f.read())!=-1){ 
					tableau[i]=(byte)ch;
					i++;						
				}
			}
			finally{
				f.close();
			}
			return tableau;
		}
		catch(FileNotFoundException ef){
			System.out.println("fichier introuvable");
			return null;
		}
		catch(IOException e){
			System.out.println(e+"erreur lors de la lecture du fichier");
			return null;			
		}		
	}