| View previous topic :: View next topic |
| Author |
Message |
Libero Spagnolini Guest
|
Posted: Thu Jun 24, 2004 11:02 am Post subject: Spedire un'immagine attraverso un socket |
|
|
Salve a tutti,
sto cercando di spedire un'immagine attraverso un socket; il client
seleziona un file e lo trasferisce al server, il quale a sua volta
dovrebbe creare una copia locale sul suo disco.
Ho un oggetto File contenente l'immagine. Sono riuscito a ottenere un
array di byte del file immagine; riesco a trasferire l'array di byte
correttamente ma ora ho la necessità di creare fisicamente un file sul
disco del server. Vorrei sapere come e se è possibile creare un file su
disco a partire da un byte[].
Grazie
ciao
--
+------------------------------------------+
Libero Spagnolini
E-mail: [email]lspagnolini (AT) iago (DOT) polito.it[/email]
Fara Novarese 28073 - NO
Italy
+------------------------------------------+
She is the insta-cure, she could cheer up
Nick Drake. And he's dead.
Adam Duritz
+------------------------------------------+
|
|
| Back to top |
|
 |
_Mario_ Guest
|
Posted: Thu Jun 24, 2004 11:16 am Post subject: Re: Spedire un'immagine attraverso un socket |
|
|
Sei certo che sulla coket non si possa sparare già il byte[]?
"Libero Spagnolini" <lspagnolini (AT) acm (DOT) org> wrote
| Quote: | Salve a tutti,
sto cercando di spedire un'immagine attraverso un socket; il client
seleziona un file e lo trasferisce al server, il quale a sua volta
dovrebbe creare una copia locale sul suo disco.
Ho un oggetto File contenente l'immagine. Sono riuscito a ottenere un
array di byte del file immagine; riesco a trasferire l'array di byte
correttamente ma ora ho la necessità di creare fisicamente un file sul
disco del server. Vorrei sapere come e se è possibile creare un file su
disco a partire da un byte[].
Grazie
ciao
--
+------------------------------------------+
Libero Spagnolini
E-mail: [email]lspagnolini (AT) iago (DOT) polito.it[/email]
Fara Novarese 28073 - NO
Italy
+------------------------------------------+
She is the insta-cure, she could cheer up
Nick Drake. And he's dead.
Adam Duritz
+------------------------------------------+
|
|
|
| Back to top |
|
 |
Cristiano Sadun Guest
|
Posted: Thu Jun 24, 2004 11:43 am Post subject: Re: Spedire un'immagine attraverso un socket |
|
|
Libero Spagnolini <lspagnolini (AT) acm (DOT) org> wrote in news:2jvqp7F16q28hU1@uni-
berlin.de:
| Quote: | Vorrei sapere come e se è possibile creare un file su
disco a partire da un byte[].
|
java.util.ByteArrayInputStream
|
|
| Back to top |
|
 |
Cristiano Sadun Guest
|
Posted: Thu Jun 24, 2004 11:44 am Post subject: Re: Spedire un'immagine attraverso un socket |
|
|
Cristiano Sadun <cristianoTAKEsadun (AT) THIShotmailOUT (DOT) com> wrote in
news:Xns95128BA7146FAcristianosadunhotmai (AT) 130 (DOT) 133.1.4:
| Quote: | Libero Spagnolini <lspagnolini (AT) acm (DOT) org> wrote in
news:2jvqp7F16q28hU1@uni- berlin.de:
Vorrei sapere come e se è possibile creare un file su
disco a partire da un byte[].
java.util.ByteArrayInputStream
|
Pardon, java.io
|
|
| Back to top |
|
 |
Vincent Vega Guest
|
Posted: Thu Jun 24, 2004 7:13 pm Post subject: Re: Spedire un'immagine attraverso un socket |
|
|
Libero Spagnolini wrote:
| Quote: | Vorrei sapere come e se è possibile creare un file su
disco a partire da un byte[].
|
[url]http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileOutputStream.html#write(byte[/url][])
|
|
| Back to top |
|
 |
Libero Spagnolini Guest
|
Posted: Thu Jun 24, 2004 10:11 pm Post subject: Re: Spedire un'immagine attraverso un socket |
|
|
Grazie a tutti per i consigli, ho risolto per spedire l'immagine:
Socket s = new Socket(InetAddress.getByName(host), port);
byte[] iBytes = getBytesFromFile(image);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.write(iBytes);
e per riceverla:
try {
ServerSocket s = new ServerSocket(5000);
while (true) {
Socket accept = s.accept();
File outputFile = new File("pippo.jpg");
InputStream is = accept.getInputStream();
FileOutputStream fos = new FileOutputStream(outputFile);
DataInputStream dis = new DataInputStream(is);
int c;
while ((c = dis.read()) != -1)
fos.write(c);
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset,
bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file
"+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
--
+------------------------------------------+
Libero Spagnolini
E-mail: [email]lspagnolini (AT) iago (DOT) polito.it[/email]
Fara Novarese 28073 - NO
Italy
+------------------------------------------+
She is the insta-cure, she could cheer up
Nick Drake. And he's dead.
Adam Duritz
+------------------------------------------+
|
|
| Back to top |
|
 |
|