In realtà avrei una curiosità e un consiglio da chiedervi.
Sempre nella stessa applicazione JSF ho aggiunto delle righe di codice al mio MyBean che di seguito vi riporto per cominciare a gestire la fase di archiviazione del file che ho dato in upload.
In realtà quello che devo fare è questo:
dovrei memorizzare in un array di byte questo file per poi inserirlo così com'è in un campo blob di una tabella.
La cosa importante è che però,dovendo eseguire in un secondo momento una query su questo campo blob devo essere in grado di "ricostruire" il file per effettuarne la query.
Avrei bisogno quindi di convertire il file in un array di byte ma secondo un formato interno mio,che mi consenta l'operazione inversa.Per fare ciò penso siano utili il numero di righe e di colonne calcolate precedentemente.
Come mi consigliate di agire?
Sono molto inesperto,quindi non ho ancora le idee chiare.
Avevo pensato,come vedrete nel bean che vi posto,di memorizzare il file in una HashMap,ma poi?
Come la converto in un array di byte ricostruibile in fase di query?
Sto trovando molti problemi a livello di strategia,se qualcuno può aiutarmi vi ringrazio in anticipo.
Ecco il bean che ho modificato:
codice:
package com.devsphere.articles.jsfupload;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.*;
public class MyBean {
private UploadedFile myFile;
private Map <String, double[]>dataMap=null;
private int row=0;
private int col=0;
private int numberOfNumericColumns =0;
public UploadedFile getMyFile() {
return myFile;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}
public String processMyFile() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(myFile.getInputStream()));
this.dataMap = new HashMap<String, double[]>();
System.out.println("Caricamento dell'array di double in corso.....");
// Store each line of the input file as a separate key and entry in the Map.
String line = null;
while ((line = br.readLine()) != null) {
line = line.replace (',', '.');
row++;
// Create a tokenizer for the line.
StringTokenizer st = new StringTokenizer(line);
// Assuming that the first column of every row is a String and
//the remaining columns are numbers, count the number of numeric columns.
numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);
// Get the first token from the line. It will be a String and
//its value will be a unique key for the rest of the row.
String key = st.nextToken().trim();
// Create the array for the numbers which make up the rest of the line.
double[] array = new double[numberOfNumericColumns];
// Populate the array by parsing the rest of the line.
for (int column = 0; column < numberOfNumericColumns; column++){
array[column] = Double.parseDouble(st.nextToken().trim());
}
// Store the first column as the key and the array as the entry.
this.dataMap.put(key, array); //Associates the specified value with
//the specified key in this map.
}
return "success";
}
}