Image Upload Image Preview - java

Hi everyone hope you can help me to display the preview image or thumbnail after selecting the image not after pressing the upload button. Hope you can help me guys! Thanks..
Profile Image Upload

This is my code for upload handler
here temp_storage_path is local application temp path
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler;
public class ImageUploadFinishedHandler implements UploadFinishedHandler {
VerticalLayout imageLayout;
public ImageUploadFinishedHandler(VerticalLayout imageLayout) {
this.imageLayout = imageLayout;
}
#Override
public void handleFile(InputStream stream, String fileName, String arg2, long arg3) {
File file = null;
try {
file = new File("temp_storage_path"+fileName);
OutputStream outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
} catch (FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
this.imageLayout.removeAllComponents();
Image previewImage = new Image();
this.imageLayout.addComponent(previewImage);
previewImage.setWidth("100px");
previewImage.setHeight("100px");
previewImage.setSource(new FileResource(file));
}
}

Related

JAVA TCP Server Error

Server : package Server;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class Server extends Thread{
private ServerSocket mServer_Socket;
private ArrayList<SocketManager> managers = new ArrayList<SocketManager>();
public Server(){
try {
mServer_Socket = new ServerSocket(4242);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
Socket msocket;
try{
msocket = mServer_Socket.accept();
System.out.println("connected");
managers.add(new SocketManager(msocket));
}catch(Exception e){
e.printStackTrace();
}
}
public void SendMessage(String m, int i){
try {
managers.get(i).write(m.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private class SocketManager{
private OutputStream mout;
private InputStream min;
public SocketManager(Socket socket){
try{
mout = socket.getOutputStream();
min = socket.getInputStream();
}catch (IOException ioe) {
ioe.printStackTrace();
}
startListen();
}
public void write(byte[] data) throws IOException{
mout.write(data);
}
public void startListen(){
new Thread() {
BufferedImage image;
public void run(){
try {
System.out.println("listen..");
while(true){
if((image = ImageIO.read(min)) != null){
while(min.read() != 'y');
System.out.println("received");
mout.write('y');
mout.flush();
Main.drawImage(image);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
Client :package Client;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.fswebcam.FsWebcamDriver;
public class Client {
private static List<Webcam> webcams = null;
static Webcam webcam = null;
static {
Webcam.setDriver(new FsWebcamDriver());
}
public static void main(String[] args) {
try {
webcams =(List<Webcam>) Webcam.getWebcams(1000000);
} catch (Exception e) {
e.printStackTrace();
}
for(Webcam device : webcams){
String name;
System.out.println(name = device.getDevice().getName());
//if(name.equals("Logitech HD Webcam C270 1"))
webcam = device;
}
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.open();
try{
Socket socket = new Socket("localhost", 4242);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
byte[] buffer = new byte[10];
while(true){
ImageIO.write(webcam.getImage(), "png", out);
out.flush();
out.write('y');
out.flush();
System.out.println("read");
while(in.read() != 'y');
}
}catch(Exception e){
e.printStackTrace();
}
}
}
This Program works well about 10sec. But after that It doesn't work. Socket is Connected but It doesn't send anything. I guess it doesn't match sync, so I match sync, but it's not work too. I don't have an idea. why It doesn't work. please help. I can't find problem
Your client needs to send the size of transfered image to server prior to sending the image itself, because your server needs to know how long the image is, in order to read it from socket and start receiving the char data coming right after the image.
And since "ImageIO" has no means of specifying the number of bytes supposed to be read from the input stream, you should use InputStream instead.
See the modified code below (I put comments whenever added a new line, everything else is identical with yours):
Server:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; //<--- added
import java.io.DataInputStream; //<--- added
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class Server extends Thread{
private ServerSocket mServer_Socket;
private ArrayList<SocketManager> managers = new ArrayList<SocketManager>();
public Server(){
try {
mServer_Socket = new ServerSocket(4242);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
Socket msocket;
try{
msocket = mServer_Socket.accept();
System.out.println("connected");
managers.add(new SocketManager(msocket));
}catch(Exception e){
e.printStackTrace();
}
}
public void SendMessage(String m, int i){
try {
managers.get(i).write(m.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private class SocketManager{
private OutputStream mout;
private InputStream min;
private DataInputStream din; //<--- added DataInputStream
public SocketManager(Socket socket){
try{
mout = socket.getOutputStream();
min = socket.getInputStream();
din = new DataInputStream(min); //<--- initialized DataInputStream
}catch (IOException ioe) {
ioe.printStackTrace();
}
startListen();
}
public void write(byte[] data) throws IOException{
mout.write(data);
}
public void startListen()
{
new Thread() {
BufferedImage image;
public void run(){
try {
System.out.println("listen..");
while(true)
{
int arrlen = din.readInt(); //<--- receive image size in order to prepare a buffer for it
byte[] b = new byte[arrlen]; //<--- prepare a buffer
din.readFully(b); //<--- receive image data
while(min.read() != 'y');
mout.write('y');
mout.flush();
InputStream bais = new ByteArrayInputStream(b); //<--- get ByteArrayInputStream from buffer
BufferedImage image = ImageIO.read(bais); //<--- prepare BufferedImage from ByteArrayInputStream
bais.close(); //<--- close ByteArrayInputStream
Main.drawImage(image);
}//end while true
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
Client:
import java.awt.image.BufferedImage; //<--- added
import java.io.ByteArrayOutputStream; //<--- added
import java.io.DataOutputStream; //<--- added
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.fswebcam.FsWebcamDriver;
public class Client {
private static List<Webcam> webcams = null;
static Webcam webcam = null;
static {
Webcam.setDriver(new FsWebcamDriver());
}
public static void main(String[] args) {
try {
webcams =(List<Webcam>) Webcam.getWebcams(1000000);
} catch (Exception e) {
e.printStackTrace();
}
for(Webcam device : webcams){
String name;
System.out.println(name = device.getDevice().getName());
//if(name.equals("Logitech HD Webcam C270 1"))
webcam = device;
}
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.open();
try{
Socket socket = new Socket("localhost", 4242);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
DataOutputStream dos = new DataOutputStream(out); //<--- added DataOutputStream
BufferedImage image = null; //<--- added BufferedImage to keep image from webcam
while(true){
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //<--- create ByteArrayOutputStream
image = webcam.getImage(); //<--- get BufferedImage from webcam
ImageIO.write(image, "png", baos); //<--- write image into ByteArrayOutputStream
dos.writeInt(baos.size()); //<--- send image size
dos.flush(); //<--- flush DataOutputStream
baos.close(); //<--- close ByteArrayOutputStream
ImageIO.write(image, "png", out);
out.flush();
out.write('y');
out.flush();
System.out.println("read");
while(in.read() != 'y');
}
}catch(Exception e){
e.printStackTrace();
}
}
}

Using File Dialogue Box Rather than FilePath Java

New to Java - I am trying to import a .txt file containing integers.
Eventually I want to import the integers into an arraylist and then create a frequency distribution and calculate the average.
Currently I am struggling to use the file dialogue box which is my end goal. I can import the .txt using the file path in my code shown. If any one could help me out with how to use the dialogue box instead it would be greatly appreciated!
import java.io.*;
import java.util.*;
public class Distribution {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Enter a complete file path to file you would like to open:");
String fileName = input.nextLine();
File inFile = new File(fileName);
FileReader ins = null;
try {
ins = new FileReader(inFile);
int ch;
while ((ch = ins.read()) != -1) {
System.out.print((char) ch);
}
}
catch (Exception e) {
System.out.println(e);
}
finally {
try {
ins.close();
}
catch (Exception e) {
}
}
} // main end
}
Well, It's simple and easy.
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
class DistributionUI {
public static void main(String[] args) {
System.out.println("Enter a complete file path to file you would like to open:");
final JFileChooser fchooser = new JFileChooser() {
private static final long serialVersionUID = 1L;
public void approveSelection() {
File inFile = getSelectedFile();
if (inFile.exists() ) {
FileReader ins = null;
try {
ins = new FileReader(inFile);
int ch;
while ((ch = ins.read()) != -1) {
System.out.print((char) ch);
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
ins.close();
} catch (Exception e) {
}
}
}
super.approveSelection();
}
};
fchooser.setCurrentDirectory(new File("."));
fchooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("my file", "txt");
fchooser.addChoosableFileFilter(filter);
fchooser.showOpenDialog(null);
} // main end
}

JavaFX Chart to Image to Base64 String use in PHP

i'am using JavaFX and illustrate a Chart with the LineChart concept in Javafx.
If i draw a chart, i export a screenshot of this with this code.
WritableImage image = lc.snapshot(new SnapshotParameters(), null);
File file = new File("Chart.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
}
catch (IOException e) {
//bla
}
This works perfect!
Now: is there a simple way to create this "WritableImage" image to a Base64 String? Furthermore i want to use it to reproduce this Base64-String to a PNG file in PHP.
Any Ideas?
THX
Your code need to continue with something like this:
//File file = new File("Chart.png"); -> this is already there
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int read= 0;
while( (read = fis.read(buffer)) > -1){
baos.write(buffer, 0, read);
}
fis.close();
baos.close();
byte pgnBytes [] = baos.toByteArray();
Base64.Encoder base64_enc = Base64.getEncoder();
String base64_image = base64_enc.encodeToString(pgnBytes);
This can be optimized further to write the file directly into byte array, if you do not need the graphic stored into file:
WritableImage image = lc.snapshot(new SnapshotParameters(), null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", baos);
}
catch (IOException e) {
//bla
}
byte pgnBytes [] = baos.toByteArray();
Base64.Encoder base64_enc = Base64.getEncoder();
String base64_image = base64_enc.encodeToString(pgnBytes);
}
In both cases image is stored in memory, which can cause OutOfMemory Error, if the image is too large etc.
SwingFXUtils.fromFXImage() returns a BufferedImage which can be easily converted to a Base64-String using BASE64.Encoder introduced in Java 8.
A complete working example :
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
VBox box = new VBox();
box.setStyle("-fx-background-color:RED;");
Scene scene = new Scene(box, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
createEncodedString(box);
}
private void createEncodedString(Node node) {
WritableImage image = node.snapshot(new SnapshotParameters(), null);
String base64String = encodeImageToString(SwingFXUtils.fromFXImage(image, null), "png");
System.out.println("Base64 String : " + base64String);
}
private String encodeImageToString(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
imageString = Base64.getEncoder().encodeToString(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
}

dialog box is not showing system's items when i use applet in place of frame?

My code is:
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.*;
import java.io.*;
import java.util.zip.GZIPOutputStream;
import java.util.*;
import java.awt.Checkbox;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import java.awt.CheckboxGroup;
import java.applet.*;
import javax.swing.JFileChooser;
/*
<applet code="compress1" width=200 height=200>
</applet>
*/
interface CompressionStrategy
{
public void compressFiles(FileOutputStream files,String str);
}
class ZipCompressionStrategy implements CompressionStrategy{
public void compressFiles(FileOutputStream files, String str)
{
byte[] buffer = new byte[1024];
try{
//using ZIP approach
ZipOutputStream zos = new ZipOutputStream(files);
ZipEntry ze= new ZipEntry(str);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(str);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
zos.closeEntry();
//remember close it
zos.close();
System.out.println("Done with Zip");
}
catch(IOException ex){
ex.printStackTrace();}
}
}
class GZipCompressionStrategy implements CompressionStrategy
{
public void compressFiles(FileOutputStream files, String str)
{
//using RAR approach
//FileOutputStream fos = null;
GZIPOutputStream gos = null;
FileInputStream fis = null;
try {
// fos = new FileOutputStream("e://myGzip.gzip");
gos = new GZIPOutputStream(files);
fis = new FileInputStream(str);
byte[] tmp = new byte[4*1024];
int size = 0;
while ((size = fis.read(tmp)) != -1) {
gos.write(tmp, 0, size); }
gos.finish();
System.out.println("Done with GZip...");
}
catch (IOException e) {}
finally{
try{
if(fis != null) fis.close();
if(gos != null) gos.close();
} catch(Exception ex){}
}
}
}
class CompressionContext
{
private CompressionStrategy strategy;
//this can be set at runtime by the application preferences
public void setCompressionStrategy(CompressionStrategy strategy)
{
this.strategy = strategy;
}
//use the strategy
public void createArchive(FileOutputStream files, String str)
{
strategy.compressFiles(files,str);
}
}
public class compress1 extends Applet implements ActionListener
{
int n;
Button b1;
Checkbox jcb1,jcb2;
CheckboxGroup cbg;
int r;
JFileChooser fs=new JFileChooser();
File file1;
static String filenm;
//TextField tf;
public void init()
{
//setTitle("compress");
Panel p1;
//setLayout(new FlowLayout());
p1=new Panel();
b1=new Button("compress");
cbg=new CheckboxGroup();
jcb1=new Checkbox("zip1",cbg,true);
jcb2=new Checkbox("gzip1",cbg,false);
//b1.setPreferredSize(new Dimension(100,100));
b1.addActionListener(this);
//tf=new TextField("",6);
p1.add(b1);
//p1.add(tf);
p1.add(jcb1);
p1.add(jcb2);
add(p1,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==b1)
{
r=fs.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
file1=fs.getSelectedFile();
filenm=file1.getAbsolutePath();
}
if(jcb1.getLabel()=="zip1"){
n=1;
}
else if(jcb2.getLabel()=="gzip1"){
n=2;
}
else{}
compress1.x(n);
}
}
static void x(int n)
{
CompressionContext ctx = new CompressionContext();
switch(n)
{
//ctx.setCompressionStrategy(new ZipCompressionStrategy());
case 1:
ctx.setCompressionStrategy(new ZipCompressionStrategy());
//get a list of files
try{
FileOutputStream fos = new FileOutputStream("f:\\MyFile1.zip");
//String st="f:\\MyFile1.zip";
ctx.createArchive(fos,filenm);
}
catch(IOException ex){
ex.printStackTrace();}
break;
case 2:
ctx.setCompressionStrategy(new GZipCompressionStrategy());
//get a list of files
try{
FileOutputStream fos = new FileOutputStream("f:\\MyFile1.gzip");
//String st="f:\\MyFile.zip";
ctx.createArchive(fos,filenm);
}
catch(IOException ex){
ex.printStackTrace();}
break;
case 3:
System.exit(0);
}
}
}
this is code for compress to files. When i was using frame it's properly work but in place of frame when now i'm using applet then dialog box is not showing any path element, i don't know what's happen.

Java - sending a file over socket - file isn't received fully

i am really stuck with a little project i'm doing. I am trying to send music files (only .wav) over a socket from a server to a client. Everything works perfectly fine (i think...) except that the file that is received by the client isn't complete. I can't play the file and I can see that it is a bit smaller than the one the server has. What am I doing not right?
Here is the server code:
private Socket client;
private String filename;
private TBMCAudioServer ac;
private FileInputStream fis;
private BufferedOutputStream out;
int bufferSize = 0;
FileSender(Socket client, String filename, TBMCAudioServer ac){
this.client = client;
this.filename = filename;
this.ac = ac;
}
#Override
public void run(){
ac.ex.sendMessage(client, "[#preload#]" + filename);
File dir = new File(ac.getDataFolder() + File.separator + "music");
if(!dir.exists()){
dir.mkdir();
}
File file = new File(dir, filename + ".wav");
long length = file.length();
if(length > Integer.MAX_VALUE){
logger.info("File is too large.");
}
byte[] bytes = new byte[(int) length];
try{
fis = new FileInputStream(file);
out = new BufferedOutputStream(client.getOutputStream());
} catch (IOException e){
logger.info(e.getMessage());
}
int count;
try {
while((count = fis.read(bytes,0,bytes.length)) != -1){
out.write(bytes, 0, count);
}
out.flush();
out.close();
fis.close();
} catch (IOException e) {
logger.info(e.getMessage());
}
}
and here you can see my client code:
private Socket server;
private String filename;
private AudioClient ac;
InputStream is = null;
FileOutputStream fos = null;
int bufferSize = 0;
FileReceiver(Socket server, String filename, AudioClient ac){
this.server = server;
this.filename = filename;
this.ac = ac;
}
#Override
public void run() {
try{
is = server.getInputStream();
bufferSize = server.getReceiveBufferSize();
ac.logConsole("Buffer size: " + bufferSize);
} catch (IOException ex){
ac.logConsole(ex.getMessage());
}
try{
fos = new FileOutputStream(AudioClient.util.getLineValue(3) + filename + ".wav");
} catch (FileNotFoundException e){
ac.logConsole(e.getMessage());
}
byte[] bytes = new byte[bufferSize];
int count;
try {
while((count = is.read(bytes, 0, bytes.length)) != -1){
fos.write(bytes, 0, count);
}
ac.logConsole("yay");
is.close();
fos.flush();
fos.close();
} catch (IOException e) {
ac.logConsole(e.getMessage());
}
}
Okay I managed to send the file fully so I can see it has the same size as where it came from with my new code. The only problem is that i'm sending a music file and I can't play the file that is sent. Maybe someone knows what the problem is?
Server code:
package me.Ciaran.simplefileserver;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleFileServer {
public final static int SOCKET_PORT = 13267; // you may change this
public final static String FILE_TO_SEND = "D:/server/plugins/TBMCAudioServer/music/DLTALL.wav"; // you may change this
public static void main (String [] args ) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
ServerSocket servsock = null;
Socket sock = null;
try {
servsock = new ServerSocket(SOCKET_PORT);
while (true) {
System.out.println("Waiting...");
try {
sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// send file
final File myFile= new File(FILE_TO_SEND); //sdcard/DCIM.JPG
byte[] mybytearray = new byte[8192];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try {
os = sock.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
int read;
while((read = dis.read(mybytearray)) != -1){
dos.write(mybytearray, 0, read);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done.");
}
finally {
if (bis != null) bis.close();
if (os != null) os.close();
if (sock!=null) sock.close();
}
}
}
finally {
if (servsock != null) servsock.close();
}
}
}
and here is the client code:
package me.Ciaran.simplefileclient;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class SimpleFileClient {
public final static int SOCKET_PORT = 13267; // you may change this
public final static String SERVER = "127.0.0.1"; // localhost
public final static String
FILE_TO_RECEIVED = "C:/Users/Ciaran/Documents/TESTEN/music/DLTALL.wav"; // you may change this, I give a
// different name because i don't want to
// overwrite the one used by server...
public final static int FILE_SIZE = 6022386; // file size temporary hard coded
// should bigger than the file to be downloaded
public static void main (String [] args ) throws IOException {
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket sock = null;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");
// receive file
InputStream in;
int bufferSize=0;
try {
bufferSize=sock.getReceiveBufferSize();
in=sock.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.println(fileName);
OutputStream output = new FileOutputStream(FILE_TO_RECEIVED);
byte[] buffer = new byte[bufferSize];
int read;
while((read = clientData.read(buffer)) != -1){
output.write(buffer, 0, read);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + current + " bytes read)");
}
finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (sock != null) sock.close();
}
}
}

Categories