📄 fileserviceimpl.java
字号:
package ssh2Down.serviceImpl;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.apache.struts2.ServletActionContext;
import ssh2Down.dao.Downfile;
import ssh2Down.dao.DownfileDAO;
import ssh2Down.dao.Downrecord;
import ssh2Down.dao.DownrecordDAO;
import ssh2Down.dao.Users;
import ssh2Down.dao.UsersDAO;
import ssh2Down.service.FileService;
import util.FileOperate;
public class FileServiceImpl implements FileService {
private UsersDAO usersDao;
private DownfileDAO downfileDao;
private DownrecordDAO downrecordDao;
public UsersDAO getUsersDao() {
return usersDao;
}
public void setUsersDao(UsersDAO usersDao) {
this.usersDao = usersDao;
}
public DownfileDAO getDownfileDao() {
return downfileDao;
}
public void setDownfileDao(DownfileDAO downfileDao) {
this.downfileDao = downfileDao;
}
public DownrecordDAO getDownrecordDao() {
return downrecordDao;
}
public void setDownrecordDao(DownrecordDAO downrecordDao) {
this.downrecordDao = downrecordDao;
}
public boolean upload(Downfile file, String AbsolutePath) {
// TODO Auto-generated method stub
String code=createCode();
file.setCode(code);
boolean isSave=this.saveFile(file, AbsolutePath);
if(isSave){
Users user=file.getUsers();
user=(Users) usersDao.findByExample(user).get(0);
file.setUsers(user);
downfileDao.save(file);
return true;
}
else return false;
}
private boolean saveFile(Downfile file,String AbsolutePath){
String path1 = ServletActionContext.getServletContext().getRealPath("/upload1")+"/"+file.getUsers().getUserName()+"/"+file.getCode();
File directory=new File(path1);
directory.mkdirs();
String path2=directory.getAbsolutePath()+"/"+file.getSaveName();
FileOperate.copyFile(AbsolutePath, path2);
return true;
}
// 生成一个唯 一的字符串,用于文件在服务器中存储的名称,也作为主键使用
private String createCode() {
Date now = new Date();
long seed = now.getTime();
Random ran = new Random(seed);
String[] letters = new String[] { "A", "B", "C", "D", "E", "F", "G",
"H", "I", "G", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };
StringBuffer result = new StringBuffer(30);
for (int i = 0; i < 10; i++) {
result.append(letters[ran.nextInt(26)]);
}
result.append(seed);
return result.toString();
}
public List findAllFile() {
// TODO Auto-generated method stub
List list=downfileDao.findAll();
return list;
}
public List findAllFileByFileName(String fileName) {
// TODO Auto-generated method stub
List list=downfileDao.findAllByfileName(fileName);
return list;
}
public Downfile findByCode(String code) {
// TODO Auto-generated method stub
return downfileDao.findById(code);
}
public boolean addRecord(String code, String userName) {
// TODO Auto-generated method stub
Downrecord record=new Downrecord();
Users user=(Users) usersDao.findByUserName(userName).get(0);
Downfile downfile=downfileDao.findById(code);
record.setDownDate(new Date());
record.setDownfile(downfile);
record.setUsers(user);
downrecordDao.save(record);
return true;
}
public List recordByUser(String userName) {
// TODO Auto-generated method stub
List list=downrecordDao.findByProperty("users.userName", userName);
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -