package SanWa.Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileUtils {
public static byte[] readFile(String path) {
File file = new File(path);
byte datas[] = null;
if (!file.exists()) {
datas = null;
} else {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(file);
byte data[] = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(data)) > 0) {
baos.write(data, 0, len);
}
datas = baos.toByteArray();
baos.flush();
baos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return datas;
}
public static boolean writeFile(String path, byte datas[]) {
try {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
fos.write(datas);
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
writeFile 客户端进行接收文件时调用的方法 用来接收服务端发送来的文件
readFile客户端进行文件发送时读取文件的方法
使用的是ByteArrayOutputStream用于多个的累计
PathUtils
package SanWa.Util;
public class PathUtils {
//获取路径
private static final String IMG_PATH="ChatRoom\\Image\\";
public static String getRealPath(String relativePath) {
return IMG_PATH + relativePath;
}
}
路径的获取
ScreenUtils
package SanWa.Util;
import java.awt.*;
public class ScreenUtils {
//获取电脑屏幕的宽度
public static int getScreenWidth() {
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
//获取电脑屏幕的高度
public static int getScreenHeight() {
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
}
获取电脑屏幕的宽度
获取电脑屏幕的高度
BackGroundPanel
package SanWa.UI;
import javax.swing.*;
import java.awt.*;
public class BackGroundPanel extends JPanel {
//声明图片
private Image backIcon;
public BackGroundPanel(Image backIcon) {
this.backIcon = backIcon;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//绘制背景
g.drawImage(backIcon,0,0,this.getWidth(),this.getHeight(),null);
}
}