上传图片和文件保存到数据库中(SmartUpload)


package demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import demo.sql.DaoTest;
import demo.sql.SQlConnectionTest;

public class UploadFile extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;
private PreparedStatement ps = null;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DaoTest dt = new DaoTest();
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(), req, resp);
su.setMaxFileSize(500 * 1024 * 1024);
try {
su.upload("GBK");
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Request smartRequest = su.getRequest();
String name = smartRequest.getParameter("name");

try {

int count = su.getFiles().getCount();
for (int i = 0; i < count; i++) {
File myFile = su.getFiles().getFile(i);
String fileName = myFile.getFieldName();
String path = this.getServletContext().getRealPath("/");
path = path + fileName;
int fileSize = myFile.getSize();
myFile.saveAs(path, su.SAVE_PHYSICAL);
java.io.File file = new java.io.File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(
new FileInputStream(file));

String sql = "insert into filetest (name,content) values(?,?)";
System.out.println(sql);

try {
PreparedStatement ps = dt.getConn().prepareStatement(sql);
ps.setString(1, name);
// ps.setBinaryStream(2, fis, (int) fileSize);
// ps.setBlob(2, fis,(long)fileSize);
// ps.setCharacterStream(2, isr, (int) fileSize);
ps.setBinaryStream(2, fis, (int) fileSize);
dt.getConn().setAutoCommit(true);
ps.executeUpdate();
dt.getConn().commit();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
fis.close();
isr.close();
file.delete();
try {
SQlConnectionTest.closeAll(dt.getConn(), ps, null);
ps = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}



<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<form id="myForm" method="post" action="UploadFile.action" enctype="multipart/form-data">
<table>
<tr>
<td>
用户名:<input type="text" id="name" name="name"/>
</td>
</tr>
<tr>
<td>
<input type="file" id="content" name="content"/>
</td>
</tr>
</table>
<input type="submit" id="commit" name="commit"/>
</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值