【方法一:使用LongBlob 不丢失原有格式】
http://topic.csdn.net/u/20100409/00/0620229f-7f92-464d-bca6-6db00dcc799f.html
mysql表里面搞个longblob字段保存word
代码:
1)上传
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute("insert into test(myid) values (5)");
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File("c:\\kick.jpg");
InputStream photoStream = new FileInputStream(file);
sql = " UPDATE test SET photo = ? WHERE myid = 5" ;
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int)file.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
2)下载:
PreparedStatement pst = ..... //省略获取Connection及查询的sql
ResultSet rs = pst.executeQuery();
InputStream is = rs.getBinaryStream(1); //1表示你的word字段在结果集中的索引号
FileOutputStream fos = new FileOutputStream("path");
byte [] buf = new byte[1024];
while(is.read(buf)!=-1){
fos.write(buf);
}
//close省略
mysql表里面搞个longblob字段保存word
代码:
1)上传
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute("insert into test(myid) values (5)");
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File("c:\\kick.jpg");
InputStream photoStream = new FileInputStream(file);
sql = " UPDATE test SET photo = ? WHERE myid = 5" ;
pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int)file.length());
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
2)下载:
PreparedStatement pst = ..... //省略获取Connection及查询的sql
ResultSet rs = pst.executeQuery();
InputStream is = rs.getBinaryStream(1); //1表示你的word字段在结果集中的索引号
FileOutputStream fos = new FileOutputStream("path");
byte [] buf = new byte[1024];
while(is.read(buf)!=-1){
fos.write(buf);
}
//close省略
【方法二:转换成二进制,在保存,容易丢失原有word格式】