📄 func.java
字号:
package com.upload;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.fileupload.FileItem;
import com.user.Conn;
public class Func {
private Conn db = null;
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private boolean flag;
private int amount = 0;
private List list = null;
private Vo vo;
//转码1
public String trans(String chi)
{
String result = null;
byte temp[];
try
{
temp = chi.getBytes("iso-8859-1");
result = new String(temp);
} catch (java.io.UnsupportedEncodingException e)
{
System.out.println(e.toString());
}
return result;
}
// 转码2
public String trans(Object chi)
{
return trans(chi.toString());
}
//添加
public int addFile(Vo vo,String input[]) {
String sql;
try {
FileItem item = vo.getItem();
String file_name = "";
long file_size = 0;
//String file_content_type = "";
InputStream in = null;
db = new Conn();
conn = db.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery("select max(id) from filetest");
rs.next();
int id = rs.getInt(1) + 1;
conn.setAutoCommit(false);
if (item.getName() != null && item.getName().length() > 0)
{
file_name = item.getName();
String file_name_real[] = file_name.split("\\\\");
file_name = file_name_real[file_name_real.length - 1];
file_size = item.getSize();
in = item.getInputStream();
}
sql="insert into filetest values(?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, trans(input[0]));
pstmt.setString(3, file_name);
pstmt.setBinaryStream(4, in, (int) file_size);
pstmt.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
this.amount = pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
} catch (Exception e) {
try {
this.amount = 0;
conn.rollback();
} catch (Exception e1) {
System.out.println(e1);
e1.printStackTrace();
}
System.out.println(e);
e.printStackTrace();
} finally {
db.closeConn();
}
return this.amount;
}
//获取所有库存文档
public List getList()
{
list=new ArrayList();
String sql="select id,name,addtime from filetest order by id desc";
try
{
db = new Conn();
conn=db.getConn();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
vo=new Vo();
vo.setId(String.valueOf(rs.getInt(1)));
vo.setName(rs.getString(2));
vo.setAddtime(rs.getDate(3).toString());
list.add(vo);
}
}catch(Exception e)
{
System.out.print("数据读取失败");
e.printStackTrace();
}
return list;
}
//删除单个公文信息
public boolean delete(int id)
{
flag=false;
String sql="delete from filetest where id="+id;
try{
db=new Conn();
conn=db.getConn();
stmt=conn.createStatement();
stmt.execute(sql);
flag=true;
}catch(Exception e)
{
System.out.print("删除失败");
e.printStackTrace();
}
return flag;
}
//获取单个下载公文信息
public Vo readFileData(int id) {
String sql="select name,content from filetest where id="+ id;
try {
db = new Conn();
conn = db.getConn();
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
vo=new Vo();
vo.setName(rs.getString(1));
vo.setFile_content_byte(rs.getBytes(2));
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return vo;
}
//转换成UTF8编码
public String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) {
k += 256;
}
sb.append("%" + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -