package com.zxc.struts.updownload.services;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
import bean.DBAccess;
import bean.FileInfo;
public class FileService {
DBAccess db = new DBAccess();
public boolean isExsitSameFileName(String filename,
HttpServletRequest request) {
boolean flag = false;
PreparedStatement ps = null;
ResultSet rs = null;
Connection connection = db.getConnection();
String sql = "select * from file where filename=?";
try {
ps = connection.prepareStatement(sql);
ps.setString(1, filename);
rs = ps.executeQuery();
if (rs.next()) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileId(rs.getString("fileid"));
fileInfo.setFileInfo(rs.getString("fileinfo"));
fileInfo.setFileIp(rs.getString("fileip"));
fileInfo.setFileName(rs.getString("filename"));
fileInfo.setFileSavename(rs.getString("filesavename"));
fileInfo.setFileSize((rs
.getString("filesize")));
fileInfo.setFileTag(rs.getString("filetag"));
fileInfo.setFileTimes(Integer.parseInt(rs
.getString("filetimes")));
fileInfo.setFileType(rs.getString("filetype"));
fileInfo.setFileUptime(rs.getString("fileuptime"));
java.io.File f = new java.io.File(request.getSession()
.getServletContext().getRealPath("/")
+ fileInfo.getFileSavename());
if (f.isFile())
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return flag;
}
public boolean saveFile(FileInfo fileInfo) {
boolean flag = false;
PreparedStatement ps = null;
Connection connection = db.getConnection();
String sql = "insert into file(fileId,fileSavename,fileName,fileType,fileSize,fileInfo,fileUptime,fileIp,fileTimes,fileTag) "
+ "values(?,?,?,?,?,?,?,?,?,?)";
try {
ps = connection.prepareStatement(sql);
ps.setString(1, fileInfo.getFileId());
ps.setString(2, fileInfo.getFileSavename());
ps.setString(3, fileInfo.getFileName());
ps.setString(4, fileInfo.getFileType());
ps.setString(5, String.valueOf(fileInfo.getFileSize()));
String fileString = fileInfo.getFileInfo();
ps.setString(6, fileString);
ps.setString(7, fileInfo.getFileUptime());
ps.setString(8, fileInfo.getFileIp());
ps.setString(9, String.valueOf(fileInfo.getFileTimes()));
ps.setString(10, fileInfo.getFileTag());
int i = ps.executeUpdate();
if (i == 1)
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return flag;
}
public List queryFile(HttpServletRequest request) {
boolean flag = false;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
ResultSet rs = null;
List list = new ArrayList<FileInfo>();
Connection connection = db.getConnection();
String sql1 = "select * from file order by fileuptime desc";
String sql2 = "delete from file where filesavename=?";
try {
ps = connection.prepareStatement(sql1);
rs = ps.executeQuery();
while (rs.next()) {
FileInfo fileInfo = new FileInfo();
fileInfo.setFileId(rs.getString("fileid"));
fileInfo
.setFileInfo(changehtml(formatString(rs.getString("fileinfo"), 15)));
fileInfo.setFileInfoReal(rs.getString("fileinfo"));
fileInfo.setFileIp(rs.getString("fileip"));
String filename = rs.getString("filename");
fileInfo.setFileName(filename);
fileInfo.setFileSavename(rs.getString("filesavename"));
Double filesize=Double.parseDouble(rs.getString("filesize"));
fileInfo.setFileSize(formatFileSize(filesize));
fileInfo.setFileTag(rs.getString("filetag"));
fileInfo.setFileTimes(Integer.parseInt(rs
.getString("filetimes")));
fileInfo.setFileType(rs.getString("filetype"));
fileInfo.setFileUptime(rs.getString("fileuptime"));
java.io.File f = new java.io.File(request.getSession()
.getServletContext().getRealPath("/")
+ fileInfo.getFileSavename());
if (f.isFile())
list.add(fileInfo);
else {
ps1 = connection.prepareStatement(sql2);
ps1.setString(1, fileInfo.getFileSavename());
ps1.executeUpdate();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("查询列表成功!");
return list;
}
public FileInfo findFileByName(String filesavename) {
PreparedStatement ps = null;
ResultSet rs = null;
Connection connection = db.getConnection();
FileInfo fileInfo = null;
String sql = "select * from file where filesavename=?";
try {
ps = connection.prepareStatement(sql);
ps.setString(1, filesavename);
rs = ps.executeQuery();
if (rs.next()) {
fileInfo = new FileInfo();
fileInfo.setFileId(rs.getString("fileid"));
fileInfo
.setFileInfo(formatString(rs.getString("fileinfo"), 15));
fileInfo.setFileInfoReal(rs.getString("fileinfo"));
fileInfo.setFileIp(rs.getString("fileip"));
fileInfo.setFileName(rs.getString("filename"));
fileInfo.setFileSavename(rs.getString("filesavename"));
fileInfo.setFileSize((rs
.getString("filesize")));
fileInfo.setFileTag(rs.getString("filetag"));
fileInfo.setFileTimes(Integer.parseInt(rs
.getString("filetimes")));
fileInfo.setFileType(rs.getString("filetype"));
fileInfo.setFileUptime(rs.getString("fileuptime"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return fileInfo;
}
public FileInfo findFileById(String fileid) {
PreparedStatement ps = null;
ResultSet rs = null;
Connection connection = db.getConnection();
FileInfo fileInfo = null;
String sql = "select * from file where fileid=?";
try {
ps = connection.prepareStatement(sql);
ps.setString(1, fileid);
rs = ps.executeQuery();
if (rs.next()) {
fileInfo = new FileInfo();
fileInfo.setFileId(rs.getString("fileid"));
fileInfo
.setFileInfo(formatString(rs.getString("fileinfo"), 15));
fileInfo.setFileInfoReal(rs.getString("fileinfo"));
fileInfo.setFileIp(rs.getString("fileip"));
fileInfo.setFileName(rs.getString("filename"));
fileInfo.setFileSavename(rs.getString("filesavename"));
fileInfo.setFileSize((rs
.getString("filesize")));
fileInfo.setFileTag(rs.getString("filetag"));
fileInfo.setFileTimes(Integer.parseInt(rs
.getString("filetimes")));
fileInfo.setFileType(rs.getString("filetype"));
fileInfo.setFileUptime(rs.getString("fileuptime"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return fileInfo;
}
public boolean deleteFile(String filesavename) {
boolean flag = false;
PreparedStatement ps = null;
Connection connection = db.getConnection();
String sql = "delete from file where fil
UPC_思念
- 粉丝: 290
- 资源: 40
最新资源
- DIWA353锅筒筒体的焊接工艺 - .pdf
- DLT 370-2010 承压设备焊接接头金属磁记忆检测.pdf
- DLT 678-1999 电站钢结构焊接通用技术条件.pdf
- DLT 678-2013 电力钢结构焊接通用技术条件.pdf
- DLT 734-2000 火力发电厂锅炉汽包焊接修复技术导则.pdf
- DLT 754-2001 铝母线焊接技术规程.pdf
- 基于AI外呼系统,基于自然语言处理(NLP)、语音识别(ASR)、语音合成(TTS)和通讯(freeswitch)技术,实现自动语音应答,用自然逼真的对话与客户
- DLT 754-2013 母线焊接技术规程.pdf
- DLT 819-2010 火力发电厂焊接热处理技术规程.pdf
- DLT 821-2002 钢制承压管道对接焊接接头射线检验技术规程.pdf
- DLT 868-2014 焊接工艺评定规程.pdf
- DLT 820-2002 管道焊接接头超声波检验技术规程.pdf
- DLT 1097-2008 火电厂凝汽器管板焊接技术规程.pdf
- DLT 905-2004 汽轮机叶片焊接修复技术导则.pdf
- DLT 1118-2009 核电厂常规岛焊接技术规程.pdf
- DLT 1117-2009 核电厂常规岛焊接工艺评定规程.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页