AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Spedire un'immagine attraverso un socket

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
Libero Spagnolini
Guest





PostPosted: Thu Jun 24, 2004 11:02 am    Post subject: Spedire un'immagine attraverso un socket Reply with 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
_Mario_
Guest





PostPosted: Thu Jun 24, 2004 11:16 am    Post subject: Re: Spedire un'immagine attraverso un socket Reply with quote



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





PostPosted: Thu Jun 24, 2004 11:43 am    Post subject: Re: Spedire un'immagine attraverso un socket Reply with quote



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





PostPosted: Thu Jun 24, 2004 11:44 am    Post subject: Re: Spedire un'immagine attraverso un socket Reply with quote

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





PostPosted: Thu Jun 24, 2004 7:13 pm    Post subject: Re: Spedire un'immagine attraverso un socket Reply with quote

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





PostPosted: Thu Jun 24, 2004 10:11 pm    Post subject: Re: Spedire un'immagine attraverso un socket Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.