package bean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.internal.LinkedTreeMap;
import com.sun.org.apache.regexp.internal.RE;
import java.io.*;
import java.math.RoundingMode;
import java.nio.channels.NonWritableChannelException;
import java.text.DecimalFormat;
import java.util.*;
/**
* Created by 53196 on 2017/10/27.
*/
public class jsonUtil {
private static DecimalFormat df = null;
static {
// 设置数字格式,保留一位有效小数
df = new DecimalFormat("#0.0");
df.setRoundingMode(RoundingMode.HALF_UP);
df.setMinimumFractionDigits(1);
df.setMaximumFractionDigits(1);
}
/**
* 格式化文件大小
* @param length
* @return
*/
public static String getFormatFileSize(long length) {
double size = ((double) length) / (1 << 30);
if(size >= 1) {
return df.format(size) + "GB";
}
size = ((double) length) / (1 << 20);
if(size >= 1) {
return df.format(size) + "MB";
}
size = ((double) length) / (1 << 10);
if(size >= 1) {
return df.format(size) + "KB";
}
return length + "B";
}
public static user fromJson(String s){
Gson gson = new Gson();
user u = gson.fromJson(s , user.class);
return u;
}
public static String toJson(user user){
Gson gson = new Gson();
return gson.toJson(user);
}
public static String JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
return in;
}
// 读文件,返回字符串
public static String ReadFile(String path) {
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
// System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
laststr = laststr + tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}
// 把json格式的字符串写到文件
public static boolean writeFile(String filePath, String sets) {
FileWriter fw;
try {
fw = new FileWriter(filePath);
PrintWriter out = new PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
//使用泛型的方法,恢复类型。
private static <T> List<T> StringTolist(String jsonString, Class<T[]> type) {
Gson gson = new Gson();
T[] list = gson.fromJson(jsonString, type);
return Arrays.asList(list);
}
public static boolean WriterUserMap(HashMap<String , user> userMap)
{
for (String key:userMap.keySet()){
for (int i = 0 ;i<userMap.get(key).getFileList().size();i++) {
userMap.get(key).getFileList().get(i).setFilePath("path");
}
}
Gson gson = new Gson();
return jsonUtil.writeFile(System.getProperty("user.dir")+"/src/userMap",gson.toJson(userMap));
}
public static HashMap<String , user> ReadJsonForUserMap(){
Gson gson = new Gson();
String s = jsonUtil.ReadFile(System.getProperty("user.dir")+"/src/userMap");
HashMap<String ,user> userMap = new HashMap<>();
HashMap map = gson.fromJson(s,HashMap.class);
for (Object key:map.keySet()) {
user u = new user();
LinkedTreeMap linkedTreeMap = (LinkedTreeMap)map.get(key.toString());
u.setUserName(linkedTreeMap.get("userName").toString());
u.setPassWord(linkedTreeMap.get("passWord").toString());
u.setOperate(0);
String jsonString = linkedTreeMap.get("fileList").toString();
List<file> list = jsonUtil.StringTolist(jsonString,file[].class);
ArrayList<file> fl = new ArrayList<>();
for (int i = 0;i<list.size();i++){
fl.add(list.get(i));
}
u.setFileList(fl);
userMap.put(u.getUserName(),u);
}
return userMap;
}
//获取时间
public static String time(){
String nowTime = "";
Calendar now = Calendar.getInstance();
nowTime += now.get(Calendar.HOUR_OF_DAY) +"时";
nowTime += now.get(Calendar.MINUTE) +"分";
nowTime += now.get(Calendar.YEAR) ;
nowTime += (now.get(Calendar.MONTH)+1) ;
nowTime += now.get(Calendar.DATE) ;
return nowTime;
}
public static void main(String[] args){
/*user u = new user("username1","password");
file f = new file("filename","","","0");
u.getFileList().add(f);
String s = jsonUtil.toJson(u);
System.out.println(s);
user u1 = jsonUtil.fromJson("{\"userName\":\"username233\",\"passWord\":\"password\",\"operate\":1,\"fileList\":[{\"fileName\":\"clientFile\",\"filePath\":\"\",\"fileSize\":\"\",\"filedate\":\"\"}],\"LOGIN\":1,\"UPLOAD\":2,\"DOWNLOAD\":3,\"DELETE\":4}");
System.out.println(u1.getUserName());*/
Gson gson = new Gson();
HashMap<String,user> userMap = new HashMap<>();
user u1 = new user("username","password");
user u2 = new user("leyinggang","123456");
userMap.put(u1.getUserName(),u1);
userMap.put(u2.getUserName(),u2);
jsonUtil.writeFile(System.getProperty("user.dir")+"/src/userMap",gson.toJson(userMap));
/*HashMap<String , user> map = jsonUtil.ReadJsonForUserMap();
System.out.println(map.get("username"));
*/
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
山东大学计算机网络课程设计Java实现FTP的类似的功能
共46个文件
class:19个
java:12个
xml:8个
1星 需积分: 15 13 下载量 23 浏览量
2017-11-17
15:18:09
上传
评论 2
收藏 230KB RAR 举报
温馨提示
山东大学计算机网络课程设计,利用Java实现类似于FTP的相关功能,用户登录,用户上传文件,下载文件,删除文件。使用Javasocket的作为传输底层。
资源推荐
资源详情
资源评论
收起资源包目录
NetFtp.rar (46个子文件)
NetFtp
bin
gson-2.3.1.jar 206KB
NetFtp.iml 674B
src
NetSocket
ServerHandleThread.java 2KB
test.java 545B
fileServer.java 3KB
beanClient.java 2KB
beanServer.java 1KB
fileClient.java 1KB
bean
user.java 2KB
jsonUtil.java 6KB
file.java 1KB
controll
Server.java 6KB
Controller.java 5KB
client.fxml 3KB
Main.java 691B
userMap 377B
.idea
uiDesigner.xml 9KB
gradle.xml 218B
misc.xml 489B
vcs.xml 173B
description.html 189B
modules.xml 259B
encodings.xml 171B
compiler.xml 734B
workspace.xml 75KB
artifacts
inspectionProfiles
out
production
NetFtp
NetSocket
test$1.class 621B
ServerHandleThread.class 3KB
fileClient.class 2KB
fileServer$getFile.class 2KB
test.class 889B
beanServer.class 2KB
beanClient.class 2KB
fileServer.class 2KB
bean
jsonUtil.class 7KB
file.class 2KB
user.class 3KB
controll
Controller$3.class 767B
client.fxml 3KB
Controller$1.class 1KB
Main.class 1KB
Server$1.class 711B
Server.class 4KB
Server$2.class 1KB
Controller$2.class 2KB
Controller.class 6KB
userMap 377B
共 46 条
- 1
资源评论
- lu_11102018-01-05不能用啊啊啊
天天夜明
- 粉丝: 2
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功