Why Output to JTextArea in real-time doesn't show up? - java

I use JTextArea to show status of unzipping file.
But for a reason it does not display the appended text.
Can any one suggest a solution?
public class UnzipFile extends Thread{
private static FtpTabPanel panel;
private File archive,outputDir;
public UnzipFile(File archive, File outputDir, FtpTabPanel panel) {
UnzipFile.panel = panel;
this.archive = archive;
this.outputDir = outputDir;
}
#Override
public void run() {
super.run();
unzipArchive();
}
public void unzipArchive() {
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, outputDir);
}
panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));
} catch (Exception e) {
OeExceptionDialog.show(e);
}
}
private void unzipEntry(ZipFile zipfile, final ZipEntry entry, File outputDir) {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()){
createDir(outputFile.getParentFile());
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
panel.statusTextArea.append("Extracting: " + entry + "\n");
}
});
try {
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
IOUtils.copy(inputStream, outputStream);
outputStream.close();
inputStream.close();
}catch (IOException io){
OeExceptionDialog.show(io);
}catch (NullPointerException n){
OeExceptionDialog.show(n);
}catch (ArithmeticException a){
OeExceptionDialog.show(a);
}
}
}
In the below code i use SwingWorkers but it unzip just one item from zip file and nothing appears in the jtextArea
public class UnzipWorkers extends SwingWorker<String,Void> {
private WebTextArea statusTextArea;
private File archive,outputDir;
public UnzipWorkers(WebTextArea statusTextArea,File archive,File outputDir) {
this.archive=archive;
this.outputDir=outputDir;
this.statusTextArea = statusTextArea;
}
#Override
protected String doInBackground() throws Exception {
statusTextArea.append(String.valueOf(System.currentTimeMillis()));
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
String status = unzipEntry(zipfile, entry, outputDir);
return status;
}
} catch (Exception e) {
OeExceptionDialog.show(e);
}
return null;
}
#Override
protected void done() {
super.done();
try {
statusTextArea.append( get() + "\n");
FileTreePanel.btnRefresh.doClick();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private String unzipEntry(ZipFile zipfile, final ZipEntry entry, File outputDir) {
String success = "Extracted failed: "+ entry + "\n";
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()){
createDir(outputFile.getParentFile());
}
try {
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
IOUtils.copy(inputStream, outputStream);
outputStream.close();
inputStream.close();
success="Extracted successfully: " + entry + "\n";
}catch (IOException io){
OeExceptionDialog.show(io);
}catch (NullPointerException n){
OeExceptionDialog.show(n);
}catch (ArithmeticException a){
OeExceptionDialog.show(a);
}
return success;
}
private void createDir(File dir) {
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (RuntimeException re) {
OeExceptionDialog.show(re);
}
}
}
}

Judging by this line:
panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));
you are running your code on the EDT, otherwise you'd get an IllegalThreadAccess exception. So, in effect, your whole extraction procedure is done as the handling of a single event. Your requests to update the TextArea are just being pushed to the event queue and wait there until you have done "handling" the event that triggered the extraction code.
You must run your code on a dedicated thread (use SwingWorker).

Did you try to move the
panel.statusTextArea.append(String.valueOf(System.currentTimeMillis()));
inside of the loop?

Related

Create a Fat-Jar Programmatically

I am trying to create a fat-jar programmatically. So far, I managed to create a Jar file with my code inside. The problem is that, if I run this, I receive an exception saying Cannot load user class: org.company.bla.bla.
Here is my code
private static void createJar() throws IOException {
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("events.jar"), manifest);
Path root = Paths.get(".").normalize().toAbsolutePath();
System.out.println(root.toString());
Files.walk(root).forEach(f -> add(f.toFile(), target));
target.close();
}
private static void add(File source, JarOutputStream target)
{
BufferedInputStream in = null;
try {
if (source.isDirectory()) {
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty()) {
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true) {
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
How can I also add the dependencies to this Jar? Or where is the path where the dependencies are so I can include them?

Write JSON Object to file- append not working

I'try to save my custom JSONObject into the file.Everything working correct but I can't append json into file.For example,If I click twice to save json,in my file I have one element.Here is a my source
public class TransactionFileManager {
public static final File path = Environment.
getExternalStoragePublicDirectory(Environment.getExternalStorageState() + "/myfolder/");
public static final File file = new File(path, "transaction1.json");
public static String read() {
String ret = null;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
try {
String receiveString;
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
ret = stringBuilder.toString();
bufferedReader.close();
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
try {
file.createNewFile();
} catch (IOException ioe) {
ioe.printStackTrace();
}
e.printStackTrace();
}
return ret;
}
public static void writeToFile(JSONObject data) {
if (!path.exists()) {
path.mkdirs();
}
try {
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fOut);
outputStreamWriter.append(data.toString());
outputStreamWriter.close();
fOut.flush();
fOut.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
}
How I can append json object into my .json file.What's a wrong in my code
thanks

How can I make this faster at reading and writing?

public class DataMiner {
private static BigData app = new BigData();
private static DomainOfConstants doc = new DomainOfConstants();
private static Logger log = Logger.getLogger(DataMiner.class);
private static DBManager conn = new DBManager();
private static java.sql.Connection con = null;
private static AmazonS3 s3Client;
private static Iterator<String> itr;
private static List<String> entries = new ArrayList<String>();
private static S3Object s3Object;
private static ObjectMetadata meta;
public static InputStream dataStream;
public static byte[] buffer = new byte[1024];
public static File file = new File(app.getCurrentPacsId()+".txt");
private static void obtainConnection(){
conn.connection();
entries = conn.grabDataSet();
conn.closeDb();
downloadBucket();
}
/*
*
* The Java heap size limits for Windows are:
* maximum possible heap size on 32-bit Java: 1.8 GB
* recommended heap size limit on 32-bit Java: 1.5 GB (or 1.8 GB with /3GB option)
*
* */
/*-------------Download and un-zip backup file-------------*/
private static void downloadBucket(){
try {
app.setAwsCredentials(doc.getAccessKey(), doc.getSecretKey());
s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(app.getAwsCredentials())).withRegion(Regions.US_EAST_1).build();
System.out.println("Connected to S3");
itr = entries.iterator();
while(itr.hasNext()){
app.setBucketKey(itr.next());
String key = app.getBucketKey();
app.setCurrentPacsId(key);
s3Object = s3Client.getObject(new GetObjectRequest(doc.getDesiredBucket(), app.getBucketKey()));
try {
ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent());
ZipEntry entry = zis.getNextEntry();
extractObjects(buffer, s3Client, zis, entry);
} catch (AmazonServiceException e) {
log.error(e);
} catch (SdkClientException e) {
log.error(e);
} catch (IOException e) {
log.error(e);
}
}
System.out.println("Processing complete");
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
public static void extractObjects(byte[] buffer, AmazonS3 s3Client, ZipInputStream zis, ZipEntry entry) throws IOException {
PipedOutputStream outputStream = null;
PipedInputStream is = null;
try {
while (entry != null)
{
String fileName = entry.getName();
if (fileName == "lib") {
fileName = entry.getName();
}
boolean containsBackup = fileName.contains(doc.getDesiredFile());
if (containsBackup == true) {
System.out.println("A back up file was found");
long start = System.currentTimeMillis();
formatSchemaName();
System.out.println("Extracting :" + app.getCurrentPacsId());
log.info("Extracting " + app.getCurrentPacsId() + ",
compressed: " + entry.getCompressedSize() + " bytes,
extracted: " +
entry.getSize() + " bytes");
//ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream = new PipedOutputStream();
is = new PipedInputStream(outputStream);
int len;
while ((len = zis.read(buffer)) >= 0)
{
outputStream.write(buffer, 0, len);
}
//InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
meta = new ObjectMetadata();
meta.setContentLength(file.length());
fileName = app.getCurrentPacsId();
runDataConversion(is,s3Client,fileName);
recordTime(start);
is.close();
outputStream.close();
System.out.println("Unzip complete");
}
else{
System.out.println("No back up found");
}
entry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (AmazonServiceException e) {
log.error(e);
} catch (SdkClientException e) {
log.error(e);
}
}
/*------------Formating the replacment file name---------*/
private static void formatSchemaName(){
String s3Key = app.getCurrentPacsId();
String id = s3Key.replace(".zip", ".txt");
id = id.substring(id.indexOf("_"));
id = id.replaceFirst("_", "");
app.setCurrentPacsId(id);
}
/*---------------Process the data file----------------------*/
private static void runDataConversion(PipedInputStream is, AmazonS3 s3Client, String fileName) {
DataProcessor convert = new DataProcessor(s3Client);
convert.downloadBucket(is,fileName);
}
/*-------Records execution time of program in min/sec------*/
private static void recordTime(long start) throws IOException {
long end = System.currentTimeMillis();
long minutes = TimeUnit.MILLISECONDS.toMinutes(end - start);
long seconds = TimeUnit.MILLISECONDS.toSeconds(end - start);
System.out.println("Execution speed "+ minutes + ":" + (seconds % 60) +" min/sec\n");
}
And here is the class that does some text file processing.The code is very slow overall when processing files up to 3.5gb. It takes 3 hours to do so while running. I have tried using piped streams over byte streams. Java heap size set to -xms2800m on a 64 bit JDK.
public class DataProcessor {
private static AmazonS3 s3Client;
private static ObjectMetadata meta;
private static DomainOfConstants doc = new DomainOfConstants();
private static BigData app = new BigData();
public static File file = new File(app.getCurrentPacsId()+".txt");
private static Logger log = Logger.getLogger(DataProcessor.class);
//Construct connection
public DataProcessor (AmazonS3 s3Client){
this.s3Client = s3Client;
}
//
public void downloadBucket(PipedInputStream is, String fileName) {
try {
File dataStream = dataConversion(is);
s3Client.putObject(doc.getDestinationBucket(),FilenameUtils.getFullPath(doc.getDestinationKey()) + "Modified_"+ fileName, dataStream);
} catch (AmazonServiceException e) {
e.printStackTrace();
log.error(e);
} catch (SdkClientException e) {
e.printStackTrace();
log.error(e);
}
}
//Setup reading and writing streams
public static File dataConversion(PipedInputStream stream) {
BufferedReader reader = null;
BufferedOutputStream streamOut = null;
String line;
try {
reader = new BufferedReader(new InputStreamReader(stream,doc.getFileFormat()));
streamOut = new BufferedOutputStream(new FileOutputStream(file));
meta = new ObjectMetadata();
while(( line = reader.readLine() ) != null)
{
processLine(reader, streamOut, line);
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
streamOut.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
log.error(e);
}
}
return file;
}
/*---------------------------------------Data processing------------------------------------------------*/
/*-----------Process and print lines---------*/
private static void processLine(BufferedReader reader, BufferedOutputStream streamOut, String line) {
try {
String newLine = System.getProperty("line.separator");
while (reader.ready()) {
if (line.contains(doc.getInsert())) {
handleData(streamOut, line);
} else if (line.contains(doc.getUse())) {
handleSchemaName(streamOut, line);
} else {
streamOut.write(line.toLowerCase().getBytes(Charset.forName(doc.getFileFormat()).toString()));
streamOut.write(newLine.getBytes());
}
line = reader.readLine();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
log.error(e);
} catch (IOException e) {
e.printStackTrace();
log.error(e);
}
}
/*-----------Replace-Schema-Name-----------*/
private static void handleSchemaName(BufferedOutputStream streamOut, String line) throws IOException {
line = line.replace(line, "USE " + "`" + doc.getSchemaName() + app.getCurrentPacsId() + "`;");
streamOut.write(line.getBytes(Charset.forName(doc.getFileFormat())));
}
/*--------Avoid-Formating-Data-Portion-of-file--------*/
private static void handleData(BufferedOutputStream streamOut, String line) throws IOException {
StringTokenizer tk = new StringTokenizer(line);
while (tk.hasMoreTokens()) {
String data = tk.nextToken();
if (data.equals(doc.getValue())) {
streamOut.write(data.toLowerCase().getBytes(Charset.forName(doc.getFileFormat()).toString()));
data = tk.nextToken();
while (tk.hasMoreTokens()) {
streamOut.write(data.getBytes(Charset.forName(doc.getFileFormat())));
data = tk.nextToken();
}
}
streamOut.write(line.toLowerCase().getBytes(Charset.forName(doc.getFileFormat().toString())));
streamOut.write(" ".getBytes(Charset.forName(doc.getFileFormat())));
}
}
Rule 1 is always to use a bigger buffer. 1024 is pitifully small. Try 32-64K.
You need to start the pipe reading thread before doing any writes to the pipe. In fact I'm surprised you don't get 'read end dead' errors. Does this code really work at all?
In fact get rid of the piped streams. Use a single thread and do all the processing as you go.
Get rid of the ready() test. It is an extra system call for nothing. Just read until end of stream.
Use a BufferedWriter instead of a BufferedOutputStream and stop converting all those strings to bytes (and use BufferedWriter.newLine() instead of the system property).

Client's BufferedInputStream.read() while loop never stops

I EDITED CODE
I'm making file transfer program with java
I have to send 21 files.
my code stops at Client's while loop in run()
(It doesn't print "file receive complete") <- see run() in Client
Server's CODE
class SendFileThread extends Thread {
private ServerSocket fileTransferServerSocket;
private Socket fileTransferSocket;
private BufferedReader requestReader;
private PrintWriter requestAnswerer;
private BufferedOutputStream fileWriter;
private int fileTransferPort = 12345;
public SendFileThread() {
try {
fileTransferServerSocket = new ServerSocket(fileTransferPort);
fileTransferSocket = fileTransferServerSocket.accept();
requestReader = new BufferedReader(new InputStreamReader(fileTransferSocket.getInputStream()));
fileWriter = new BufferedOutputStream(fileTransferSocket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void CloseTransferStream() {
try {
requestAnswerer.close();
requestReader.close();
fileWriter.close();
fileTransferSocket.close();
fileTransferServerSocket.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void SendFile(String filename) {
try {
File file = new File(CLIENT_PATH + "/" + filename);
BufferedInputStream fileReader = new BufferedInputStream(new FileInputStream(file));
int packet;
while((packet = fileReader.read()) != -1)
fileWriter.write(packet);
fileWriter.flush();
fileReader.close();
}
catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
//System.out.print(filename + " send complete (" + count + " times)");
}
public void ListenForRequester() {
try {
String input;
while((input = requestReader.readLine()) != null) {
if(input.equals("request file")) {
SendFile(requestReader.readLine());
}
else if(input.equals("end transfer"))
break;
else {
System.out.println("Something wrong");
}
}
}
catch(IOException ioe) {
ioe.getStackTrace();
}
}
public void run() {
ListenForRequester();
CloseTransferStream();
}
}
Client's CODE
class ReceiveFileThread extends Thread {
private Socket fileTransferSocket;
private int fileTransferPort = 12345;
private BufferedInputStream fileReader;
private PrintWriter fileRequester;
public ReceiveFileThread() {
try {
fileTransferSocket = new Socket(serverIP, fileTransferPort);
fileRequester = new PrintWriter(fileTransferSocket.getOutputStream(), true);
fileReader = new BufferedInputStream(fileTransferSocket.getInputStream());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void CloseTransferStream() {
try {
fileRequester.close();
fileReader.close();
fileTransferSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public synchronized void RequestFile(String filename) {
fileRequester.println("request file");
fileRequester.println(filename);
}
public synchronized void SendEndMsg() {
fileRequester.println("end transfer");
}
public void run() {
for(int i = 0;i < fileList.size();i++) {
String filename = (String)fileList.get(i);
RequestFile(filename);
try {
BufferedOutputStream fileWriter = new BufferedOutputStream(new FileOutputStream(new File(PROGRAM_PATH + "/" + filename)));
int packet = 0;
while((packet = fileReader.read()) > -1)
fileWriter.write(packet);
System.out.println("file receive complete");
fileWriter.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
SendEndMsg();
CloseTransferStream();
}
}
It became 5 days that this error bothers me :(
Could anyone save me from this error?
Closing the socket's output stream will close the socket, In order to send multiple files you will have to make a couple of changes.
Server:
Before you start to send a file, send that file's length.
Client:
After you receive the file's length start to read that many bytes from the input stream and save them to a file, when you're done read the next file's length.

how can i transfer multiple files in a directory from client to server through java socket? [duplicate]

This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 6 years ago.
I have created a simple client-server java socket app that can transfer a single file from client into the server through socket. What do I need to modify in my application so that I can send multiple files in a directory to the server?
this my simple client:
public void connect() {
while (!isConnected) {
try {
socket = new Socket("10.110.190.82", 7999);
outputStream = new ObjectOutputStream(socket.getOutputStream());
isConnected = true;
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Error. Server is not running\n '"+e.getMessage()+"' "+
"\nThis Client will now close.", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
public void sendFile(String sourceFilePath, String fileName) {
if(socket.isConnected()){
while (socket.isConnected()) {
fileEvent = new FileEvent_1();
String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
fileEvent.setDestinationDirectory(destinationPath);
fileEvent.setFilename(fileName);
fileEvent.setSourceDirectory(sourceFilePath);
File file = new File(sourceFilePath);
if (file.isFile()) {
try {
DataInputStream diStream = new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
fileEvent.setFileSize(len);
fileEvent.setFileData(fileBytes);
fileEvent.setStatus("Success");
} catch (Exception e) {
e.printStackTrace();
fileEvent.setStatus("Error");
}
} else {
System.out.println("path specified is not pointing to a file");
fileEvent.setStatus("Error");
}
try {
outputStream.writeObject(fileEvent);
System.out.println("Done...Going to exit");
JOptionPane.showMessageDialog(null, "Upload Success", "Success", JOptionPane.INFORMATION_MESSAGE);
Thread.sleep(3000);
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException ex){
ex.printStackTrace();
}
}
} else {
JOptionPane.showMessageDialog(null, "Socket is not connected", "FAILED", JOptionPane.ERROR_MESSAGE);
}
}
and this is my simple server, it contains a client handler and a server..
this is client_handler:
public class ClientHandler implements Runnable {
Socket socket;
PrintStream out;
private ObjectInputStream inputStream;
private FileEvent_1 fileEvent;
private File dstFile;
private FileOutputStream fileOutputStream;
ClientHandler(Socket s) {
socket = s;
}
#Override
public void run() {
try {
out = new PrintStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
downloadFile();
} catch (IOException e) {
System.out.println("PrintStream Error");
}
out.println("Hello!! I'm in!!!");
try {
socket.close();
} catch (IOException e) {
System.out.println("Failed to close, oddly...");
}
}
public void downloadFile() {
try {
fileEvent = (FileEvent_1) inputStream.readObject();
if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
System.out.println("Error occurred ..So exiting");
System.exit(0);
}
String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
if (!new File(fileEvent.getDestinationDirectory()).exists()) {
new File(fileEvent.getDestinationDirectory()).mkdirs();
}
dstFile = new File(outputFile);
fileOutputStream = new FileOutputStream(dstFile);
fileOutputStream.write(fileEvent.getFileData());
fileOutputStream.flush();
fileOutputStream.close();
System.out.println("Output file : " + outputFile + " is successfully saved ");
Thread.sleep(000);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}}
and the server:
public static void main(String[] args) {
try {
socket = new ServerSocket(port);
System.out.println("SERVER START");
System.out.println("Bound to port: " + port);
} catch (IOException e) {
System.out.println("Cannot bind to port: " + port);
System.exit(0);
}
while (true) {
try {
Socket s = socket.accept();
System.out.println("New Client: "+s.getInetAddress().toString());
(new Thread(new ClientHandler(s))).start();
} catch (IOException e) {
System.out.println("Failed to accept client");
}
}
}
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
So you get the directory listing (list of all files in the directory), loop through the resulting fList and send them one by one as you would with a single file:
for(File file : fList) {
//file sending stuff
}
You can use JFileChooser if you want see the list of directories and list of files. for example:
JFileChooser jFilechooser1= new JFileChooser(new File("."));
to select the file:
if (jFilechooser1.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
dstFile=jFilechooser1.getSelectedFile();
}

Categories

Resources