📄 fileserviceimpl.java
字号:
package service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.List;
import org.hibernate.Hibernate;
import model.TFile;
import dao.TFileDAO;
import web.FileActionForm;
/**
* 业务逻辑服务的实现类
* @author LuckyStar
*
*/
public class FileServiceImpl implements FileService {
private TFileDAO tfileDAO;
/**
* 文件上传的服务方法
*/
public void save(FileActionForm fileForm)
{
TFile tfile = new TFile();
try
{
Blob content = Hibernate.createBlob(fileForm.getFileContent().getFileData());
tfile.setFileContent(content);
}
catch (FileNotFoundException ex)
{
throw new RuntimeException(ex);
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
tfile.setFileName(fileForm.getFileContent().getFileName());
tfile.setRemark(fileForm.getRemark());
tfileDAO.save(tfile);
}
/**
* 文件下载的服务方法
*/
public void write(OutputStream os, String fileId)
{
TFile tfile = tfileDAO.findById(fileId);
try
{
System.out.println("-------write");
InputStream in = tfile.getFileContent().getBinaryStream();
byte[] b = new byte[8000];
int len = 0;
while ( (len = in.read(b)) != -1) {
os.write(b, 0, len);
os.flush();
}
os.flush();
}
catch (IOException ex)
{
throw new RuntimeException(ex);
}
catch(Exception e){
e.printStackTrace();
}
}
public String getFileName(String fileId)
{
TFile tfile = tfileDAO.findById(fileId);
return tfile.getFileName();
}
public List getAllFile()
{
return tfileDAO.findAll();
}
public void setTfileDAO(TFileDAO tfileDAO)
{
this.tfileDAO = tfileDAO;
}
public TFileDAO getTfileDAO()
{
return tfileDAO;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -