Salve a tutti
Devo creare un'applicazione client-server in cui il client invia un file e il server lo riceve e lo salva su un database MySQL. Il file viene ricevuto, ma l'applicazione si blocca quando deve salvare il contenuto nel db e non riesco a capire perchè.
Parte Client
Parte Servercodice:import java.io.*; import java.net.*; import javax.swing.*; public class Upload{ public static void main(String[] args){ try{ Socket sDati= new Socket("LocalHost",12345); String linea; BufferedReader inp=new BufferedReader(new InputStreamReader(sDati.getInputStream())); DataOutputStream outS= new DataOutputStream(sDati.getOutputStream()); JFileChooser scegliFile=new JFileChooser(); scegliFile.showOpenDialog(null); File fileUp=scegliFile.getSelectedFile(); outS.writeBytes("Pippo\n"); outS.writeBytes(fileUp.getName()+"\n"); outS.writeLong(fileUp.length()); while((linea=inp.readLine())!=null){ outS.writeBytes(linea+"\n"); } outS.close(); System.out.println("finito"); } catch(Exception e){ System.out.println("Err "+e.getMessage()); } } }
Grazie in anticipocodice:import java.io.*; //per aprire gli stream import java.net.*; //per creare i socket import java.util.*; //per StringTokenizer import java.sql.*; //per interagire con il db import javax.swing.*; //per JOptionPane public class Receive{ public static void main(String[] args) throws Exception{ try{ Connection c = null; Statement st = null; try{ //scelta del driver Class.forName("com.mysql.jdbc.Driver").newInstance(); try{ //connessione al db c = DriverManager.getConnection("jdbc:mysql://localhost:3306/progetto?","root",""); st = c.createStatement(); ServerSocket sClient= new ServerSocket(12345); for(int i=1;;i++){ Socket sDati= sClient.accept(); System.out.println("client "+i+"..."); BufferedReader in=new BufferedReader(new InputStreamReader(sDati.getInputStream())); DataInputStream input= new DataInputStream(sDati.getInputStream()); String user=in.readLine(); String fileName=in.readLine(); long fileLength=input.readLong(); PreparedStatement pstmt = c.prepareStatement("INSERT INTO files (nome_utente, contenuto) "+"VALUES (?, ?)"); pstmt.setString(1, user); pstmt.setBinaryStream (2, sDati.getInputStream(), (int)fileLength); pstmt.executeUpdate(); in.close(); pstmt.close(); } } catch(SQLException e){ System.out.println("Errore di connessione al database"); } } catch(Exception e){ System.out.println("Err "+e.getMessage()); } } catch(Exception e){ System.out.println("Err "+e.getMessage()); } } }

Rispondi quotando