Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.j256.ormlite.jdbc;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- public class Client {
- public static void main(String[] args) throws Exception {
- BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Enter File Name :");
- System.out.flush();
- String fileName = console.readLine();
- // see if the file exists up front
- File file = new File(fileName);
- // turn this into a file in the current directory -- you prolly don't want this
- file = new File(file.getName());
- // see if it exists _before_ we connect
- if (file.exists()) {
- String Option;
- System.out.println("File '" + fileName + "' already exists. Want to overWrite (Y/N) ?");
- System.out.flush();
- Option = console.readLine();
- if (Option == "N") {
- return;
- }
- }
- // connect to the server
- Socket socket = new Socket(InetAddress.getLocalHost(), 8080);
- BufferedInputStream reader = new BufferedInputStream(socket.getInputStream());
- PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
- writer.println(fileName);
- int val = reader.read();
- if (val == 0) {
- System.out.println("File not found on Server ...");
- } else if (val == 1) {
- System.out.println("Receiving File ...");
- FileOutputStream fileout = new FileOutputStream(file);
- byte[] bytes = new byte[1024];
- int byteCount = 0;
- while (true) {
- int numRead = reader.read(bytes);
- if (numRead <= 0) {
- break;
- }
- byteCount += numRead;
- fileout.write(bytes, 0, numRead);
- }
- fileout.close();
- System.out.println("Received " + byteCount + " bytes to file " + fileName);
- } else {
- System.err.println("Val == " + val);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement