Sending path through Java NiOIs Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Iterate through a HashMapHow do I convert a String to an int in Java?Creating a memory leak with Java
How to judge a Ph.D. applicant that arrives "out of thin air"
Why is 'n' preferred over "n" for output streams?
Sci fi story: Clever pigs that help a galaxy lawman
If my pay period is split between 2 calendar years, which tax year do I file them in?
What is the difference between position, displacement, and distance traveled?
What is the most efficient way to write 'for' loops in Matlab?
Request for a Latin phrase as motto "God is highest/supreme"
Why does Canada require mandatory bilingualism in all government posts?
Is there an antonym for "spicy" or "hot" regarding food (NOT "seasoned" but "spicy")?
Why force the nose of 737 Max down in the first place?
What are the different qualities of the intervals?
How to tar a list of directories only if they exist
What does "see" in "the Holy See" mean?
Am I allowed to use personal conversation as a source?
How did the SysRq key get onto modern keyboards if it's rarely used?
What do I do with a party that is much stronger than their level?
Commercial jet accompanied by small plane near Seattle
Why can't my huge trees be chopped down?
Can a table be formatted so that math mode is in some columns and text is in others by default?
How to avoid theft of potentially patentable IP when trying to obtain a Ph.D?
Are there any examples of technologies have been lost over time?
Checking if an integer is a member of an integer list
Correlation length anisotropy in the 2D Ising model
Can anyone give a concrete example to illustrate what is an uniform prior?
Sending path through Java NiO
Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Does a finally block always get executed in Java?What is the difference between public, protected, package-private and private in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?Iterate through a HashMapHow do I convert a String to an int in Java?Creating a memory leak with Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am programming a server and a client, the client will send a file to the server, however, I need to send the file name together, so that the server recognizes and write the file with the name sent by the client. Currently it does not send the file name, so I must inform the server of the file name and its path where it will be saved.
Is it possible to send the file name and path that will be saved to the file?
And if it is possible, what code snippet should I use to be able to point out such information?
If Anyone can help me, I really appreciate it.
client side
public class FileSenderClient
private void sendFile(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\SIS\database.FDB");\ path to file
FileChannel inChannel = FileChannel.open(path);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) > 0)
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
private SocketChannel CreateChannel() throws IOException
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(true);
SocketAddress sockAddr = new InetSocketAddress("localhost", 9000);
socketChannel.connect(sockAddr);
return socketChannel;
public static void main(String[] args) throws IOException
FileSenderClient client = new FileSenderClient();
SocketChannel socketChannel = client.CreateChannel();
client.sendFile(socketChannel);
server side
public class FileReceiver
private void readFileFromSocketChannel(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\Data\database.FDB"); - here i need to receive
the name from the class FileSenderClient
FileChannel fileChannel = FileChannel.open(path,
EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE)
);
//Allocate a ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) > 0)
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
fileChannel.close();
System.out.println("Receving file successfully!");
socketChannel.close();
private SocketChannel createServerSocketChannel() throws IOException
ServerSocketChannel serverSocket = null;
SocketChannel client = null;
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
client = serverSocket.accept();
System.out.println("connection established .." + client.getRemoteAddress());
return client;
public static void main(String[] args) throws IOException
FileReceiver server = new FileReceiver();
SocketChannel socketChannel = server.createServerSocketChannel();
server.readFileFromSocketChannel(socketChannel);
java sockets nio
add a comment |
I am programming a server and a client, the client will send a file to the server, however, I need to send the file name together, so that the server recognizes and write the file with the name sent by the client. Currently it does not send the file name, so I must inform the server of the file name and its path where it will be saved.
Is it possible to send the file name and path that will be saved to the file?
And if it is possible, what code snippet should I use to be able to point out such information?
If Anyone can help me, I really appreciate it.
client side
public class FileSenderClient
private void sendFile(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\SIS\database.FDB");\ path to file
FileChannel inChannel = FileChannel.open(path);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) > 0)
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
private SocketChannel CreateChannel() throws IOException
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(true);
SocketAddress sockAddr = new InetSocketAddress("localhost", 9000);
socketChannel.connect(sockAddr);
return socketChannel;
public static void main(String[] args) throws IOException
FileSenderClient client = new FileSenderClient();
SocketChannel socketChannel = client.CreateChannel();
client.sendFile(socketChannel);
server side
public class FileReceiver
private void readFileFromSocketChannel(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\Data\database.FDB"); - here i need to receive
the name from the class FileSenderClient
FileChannel fileChannel = FileChannel.open(path,
EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE)
);
//Allocate a ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) > 0)
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
fileChannel.close();
System.out.println("Receving file successfully!");
socketChannel.close();
private SocketChannel createServerSocketChannel() throws IOException
ServerSocketChannel serverSocket = null;
SocketChannel client = null;
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
client = serverSocket.accept();
System.out.println("connection established .." + client.getRemoteAddress());
return client;
public static void main(String[] args) throws IOException
FileReceiver server = new FileReceiver();
SocketChannel socketChannel = server.createServerSocketChannel();
server.readFileFromSocketChannel(socketChannel);
java sockets nio
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41
add a comment |
I am programming a server and a client, the client will send a file to the server, however, I need to send the file name together, so that the server recognizes and write the file with the name sent by the client. Currently it does not send the file name, so I must inform the server of the file name and its path where it will be saved.
Is it possible to send the file name and path that will be saved to the file?
And if it is possible, what code snippet should I use to be able to point out such information?
If Anyone can help me, I really appreciate it.
client side
public class FileSenderClient
private void sendFile(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\SIS\database.FDB");\ path to file
FileChannel inChannel = FileChannel.open(path);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) > 0)
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
private SocketChannel CreateChannel() throws IOException
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(true);
SocketAddress sockAddr = new InetSocketAddress("localhost", 9000);
socketChannel.connect(sockAddr);
return socketChannel;
public static void main(String[] args) throws IOException
FileSenderClient client = new FileSenderClient();
SocketChannel socketChannel = client.CreateChannel();
client.sendFile(socketChannel);
server side
public class FileReceiver
private void readFileFromSocketChannel(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\Data\database.FDB"); - here i need to receive
the name from the class FileSenderClient
FileChannel fileChannel = FileChannel.open(path,
EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE)
);
//Allocate a ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) > 0)
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
fileChannel.close();
System.out.println("Receving file successfully!");
socketChannel.close();
private SocketChannel createServerSocketChannel() throws IOException
ServerSocketChannel serverSocket = null;
SocketChannel client = null;
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
client = serverSocket.accept();
System.out.println("connection established .." + client.getRemoteAddress());
return client;
public static void main(String[] args) throws IOException
FileReceiver server = new FileReceiver();
SocketChannel socketChannel = server.createServerSocketChannel();
server.readFileFromSocketChannel(socketChannel);
java sockets nio
I am programming a server and a client, the client will send a file to the server, however, I need to send the file name together, so that the server recognizes and write the file with the name sent by the client. Currently it does not send the file name, so I must inform the server of the file name and its path where it will be saved.
Is it possible to send the file name and path that will be saved to the file?
And if it is possible, what code snippet should I use to be able to point out such information?
If Anyone can help me, I really appreciate it.
client side
public class FileSenderClient
private void sendFile(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\SIS\database.FDB");\ path to file
FileChannel inChannel = FileChannel.open(path);
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (inChannel.read(buffer) > 0)
buffer.flip();
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
private SocketChannel CreateChannel() throws IOException
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(true);
SocketAddress sockAddr = new InetSocketAddress("localhost", 9000);
socketChannel.connect(sockAddr);
return socketChannel;
public static void main(String[] args) throws IOException
FileSenderClient client = new FileSenderClient();
SocketChannel socketChannel = client.CreateChannel();
client.sendFile(socketChannel);
server side
public class FileReceiver
private void readFileFromSocketChannel(SocketChannel socketChannel) throws IOException
Path path = Paths.get("C:\Data\database.FDB"); - here i need to receive
the name from the class FileSenderClient
FileChannel fileChannel = FileChannel.open(path,
EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE)
);
//Allocate a ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) > 0)
buffer.flip();
fileChannel.write(buffer);
buffer.clear();
fileChannel.close();
System.out.println("Receving file successfully!");
socketChannel.close();
private SocketChannel createServerSocketChannel() throws IOException
ServerSocketChannel serverSocket = null;
SocketChannel client = null;
serverSocket = ServerSocketChannel.open();
serverSocket.socket().bind(new InetSocketAddress(9000));
client = serverSocket.accept();
System.out.println("connection established .." + client.getRemoteAddress());
return client;
public static void main(String[] args) throws IOException
FileReceiver server = new FileReceiver();
SocketChannel socketChannel = server.createServerSocketChannel();
server.readFileFromSocketChannel(socketChannel);
java sockets nio
java sockets nio
edited Mar 26 at 18:36
Bruno Manica
asked Mar 26 at 18:20
Bruno ManicaBruno Manica
62 bronze badges
62 bronze badges
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41
add a comment |
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363879%2fsending-path-through-java-nio%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363879%2fsending-path-through-java-nio%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are dozens of ways to do this. However, they all require you to have a good grip on "Java Sockets". Judging from the way you posed your question, most of this code is copied and pasted from somewhere else, making it hard for you to understand the basic mechanism on which your server works.
– Dylan
Mar 26 at 18:41