本帖最后由 ~与狼共舞~ 于 2013-5-30 18:07 编辑
- import java.io.*;
- import java.net.Socket;
- import java.util.Scanner;
- public class Demo3 {
-
- public static void main(String[] args) throws Exception {
- File file = getFile();
- Socket socket = new Socket("192.168.1.133",52614);
- BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintStream ps = new PrintStream(socket.getOutputStream());
- ps.println(file.getName());
- String s = br.readLine();
- if("已存在".equals(s)){
- System.out.println("您要上传的文件已存在");
- return;
- }else{
- FileInputStream fis = new FileInputStream(file);
- OutputStream os = socket.getOutputStream();
- byte[] buffer = new byte[1024];
- int len;
- while ((len = fis.read(buffer)) != -1)
- os.write(buffer, 0, len);
- fis.close();
- socket.close();
- }
- }
- private static File getFile() {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入文件夹路径:");
- while (true) {
- File file = new File(sc.nextLine());
- if (!file.exists()) {
- System.out.println("您输入的文件夹路径不存在");
- }else if (file.isDirectory()) {
- show(file);
-
- } else {
- return file;
- }
- }
- }
- private static void show(File file) {
- file.listFiles();
- for (File f : file.listFiles()) {
- if (f.isFile()) {
- return;
- } else if(f.isDirectory()){
- show(f);
- return;
- }
-
- }
- }
- }
复制代码- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Demo4 {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(52614);
- while (true) {
- final Socket socket = ss.accept();
- new Thread() {
- public void run() {
- try {BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintStream ps = new PrintStream(socket.getOutputStream());
- String name = br.readLine();
- File file = new File("F:\\辅助",name);
- if(file.exists()){
- ps.println("已存在");
- return;
- }else{
- ps.println("不存在");
- }
- InputStream is = socket.getInputStream();
- FileOutputStream fos = new FileOutputStream(file,true);
- byte[] buffer = new byte[1024];
- int len;
- while ((len = is.read(buffer)) != -1)
- fos.write(buffer, 0, len);
- fos.close();
- socket.close();
- }catch(Exception e){
-
- }
- };
- }.start();
- }
- }
- }
复制代码 为什么我的这段代码不能读取文件夹,是不是递归的地方出了问题,哪位大侠帮忙看看。 |