1、编程实现一个时间服务器, 客户端请求服务端, 服务端返回当前时间给客户端。
2、编程实现将E:\Downloads\test.txt拷贝了D:\Downloads\下面, 目前Downloads文件夹不存在。
package com.test.xsj.controller;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
@author xsj
@date 2022/6/20 15:43
/
@RestController
@RequestMapping("/api/time")
@Log4j2
public class TimeController {
/**
/**
FileInputStream in = new FileInputStream("E:/Download/test/test.txt");
FileOutputStream out = new FileOutputStream("E:/Download/test/data/aaa.txt");
byte[] bytearray = new byte[1024];
do {
in.read(bytearray, 0, 1024);
out.write(bytearray);
} while (in.available() > 0);
in.close();
out.close();
result.put("result", "成功");
return result;
}catch (Exception e){ log.error("异常:",e);//打印异常信息
result.put("result", "失败");
return result;
}}
@RequestMapping(value = "/getTime", method = {RequestMethod.GET, RequestMethod.POST}) public Date getTime() { return new Date(); } public void fileCopy() throws Exception{ String oldPath="E:\\Downloads\\test.txt"; String newPath="D:\\Downloads\\test.txt"; String directory="D:\\Downloads\\"; File oldFile = new File(oldPath); File directoryFile = new File(directory); File newFile = new File(newPath); directoryFile.mkdir(); try (FileInputStream fis = new FileInputStream(oldFile); FileChannel input = fis.getChannel(); FileOutputStream fos = new FileOutputStream(newFile); FileChannel output = fos.getChannel()) { final long size = input.size(); // TODO See IO-386 long pos = 0; long count = 0; while (pos < size) { final long remain = size - pos; count = remain > 30*1024* 1024? 30*1024* 1024 : remain; final long bytesCopied = output.transferFrom(input, pos, count); if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size break; // ensure we don't loop forever } pos += bytesCopied; } } }
这样
本地提前新建好Downloads文件夹
下面的用到文件流的读写等
l.socket编程
2. 可以先用File抽象路径对象可以mkdir创建文件夹
然后拷贝