问个上传文件并保存到数据库的问题

daocha 2008-06-05 08:16:34
我用SMARTUPLOAD上传一个WORD 并在响应页面将上传的附件写入数据库中
可是有的时候上传的页面无法响应 当我关闭IE的时候 发现数据库链接没有关闭 我是在try{}中建立连接的
try{
conn=ConnectDB.connect();
}catch(){}
finally{
if(conn!=null)
conn.close();
}

简略的这么写 强制关闭页面为什么不执行finally呢?
...全文
203 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
daocha 2008-06-06
  • 打赏
  • 举报
回复
没有 我上传的文件才几十KB 有的时候成功 有的时候上传就没响应 被迫关闭显示器 然后再登录进去 就进不去那个页面了 因为那个连接没断开
我想完成从A页面提交附件 B页面接收附件 然后在B页面将文件上传 并且将这个文件写入数据库 然后关闭数据库连接 把文件删除 就是这么一个操作流程


<%!
//这儿需要修改成您的数据库连接信息
public Connection conn = null;
public String docid = "";
public String filename = "";
public int filesize = 0;
%>
<%
String js = null;
Userinfo userinfo = null;
String stu_id = null;
String tch_id = null;
stu_id = request.getParameter("stuid");
int xtzl = 0;
int wxzs = 0;
int zy = 0;
int bysjzl = 0;
try {
xtzl = Integer.parseInt(request.getParameter("xtzl"));
}
catch (Exception e) {
xtzl = 0;
}
try {
wxzs = Integer.parseInt(request.getParameter("wxzs"));
}
catch (Exception e) {
wxzs = 0;
}
try {
zy = Integer.parseInt(request.getParameter("zy"));
}
catch (Exception e) {
zy = 0;
}
try {
bysjzl = Integer.parseInt(request.getParameter("bysjzl"));
}
catch (Exception e) {
bysjzl = 0;
}
int op = -1;
if (request.getParameter("op") != null)
op = Integer.parseInt(request.getParameter("op"));
mySmartUpload.initialize(pageContext);
try {
mySmartUpload.upload();
mySmartUpload.save("/thesisdocuments/bysjpyjs");
}
catch (SmartUploadException e) {
out.println(MsgLang.uploaderr());
}
coghatool.wordprocess.File myFile = null;
docid = mySmartUpload.getRequest().getParameter("docid");
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
filename = myFile.getFileName();
filesize = myFile.getSize();
if (myFile.getFieldName().equalsIgnoreCase("EDITFILE")) { //正文文件
/*System.out.println("处理正文文件");
System.out.println("filename=" + filename);
System.out.println("filesize=" + filesize);
System.out.println("myFile.getFieldName()=" + myFile.getFieldName());*/
String filename_path = application.getRealPath("/") + "thesisdocuments\\bysjpyjs\\" + filename;
java.io.File tfile = null;
java.io.InputStream inStream = null;
PreparedStatement ps = null;
Statement stmt = null;
try {
int count = 0;
tfile = new java.io.File(filename_path);
inStream = new java.io.FileInputStream(tfile);
conn = ConnectDB.ReturnConnection();
if (op == 1) { //另存
String sql = "insert into t_bysjpyjs(stu_id,filename,filesize,filedata,xtzl,wxzs,zy,bysjzl,date) values(?,?,?,?,?,?,?,?,getdate())";
int total = xtzl + wxzs + zy + bysjzl;
String str2 = "update t_stuinfo set py_thesis_score=" + total + " where stu_id='" + stu_id + "';";
ps = conn.prepareStatement(sql);
stmt = conn.createStatement();
ps.setString(1, stu_id);
ps.setString(2, filename);
ps.setInt(3, filesize);
ps.setBinaryStream(4, inStream, inStream.available());
ps.setInt(5, xtzl);
ps.setInt(6, wxzs);
ps.setInt(7, zy);
ps.setInt(8, bysjzl);
count = ps.executeUpdate();
stmt.executeUpdate(str2);
}
else if (op == 0) { //保存
String strSql = "";
strSql = "update t_bysjpyjs set filename=?,filesize=?,filedata=?,xtzl=?,wxzs=?,zy=?,bysjzl=?,date=getdate() where stu_id='" + stu_id + "'";
int total = xtzl + wxzs + zy + bysjzl;
String str2 = "update t_stuinfo set py_thesis_score=" + total + " where stu_id='" + stu_id + "';";
ps = conn.prepareStatement(strSql);
stmt = conn.createStatement();
ps.setString(1, filename);
ps.setInt(2, filesize);
ps.setBinaryStream(3, inStream, inStream.available());
ps.setInt(4, xtzl);
ps.setInt(5, wxzs);
ps.setInt(6, zy);
ps.setInt(7, bysjzl);
count = ps.executeUpdate();
stmt.executeUpdate(str2);
}
if (count > 0) {
out.println(MsgLang.uploadsuccess(filename, filesize));
}
else {
out.println(MsgLang.uploadfailed2());
}
}
catch (Exception e) {
e.printStackTrace();
out.println("发生错误: " + e.toString());
}
finally {
try {
if (stmt != null)
stmt.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
if (inStream != null)
inStream.close();
if (tfile != null)
tfile.delete();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
%>
胡矣 2008-06-06
  • 打赏
  • 举报
回复
页面发送请求
server返回响应
只能这么交互
关闭页面无法控制server啊
一板砖夯死你 2008-06-06
  • 打赏
  • 举报
回复
可能是上传的文件太大,数据库的响应相对比较慢而已
可以将文件上传到某个目录,再把文件的相对路径保存到数据库中
一板砖夯死你 2008-06-06
  • 打赏
  • 举报
回复
ps.setBinaryStream(4, inStream, inStream.available())感觉是这里的问题
保存文件到数据库中应该也要和读取文件一样,要将文件以字节流的方式写入到数据库中

另外,你的页面代码和业务代码能不能分开写……
huayiluo 2008-06-05
  • 打赏
  • 举报
回复
我不知道你想实现什么功能,但是有一点。你上传一个数据也是很简单的啊。
如果你用servlet,然后在里面调用一个插入的方法不就完事了?
daocha 2008-06-05
  • 打赏
  • 举报
回复
可我是上传数据啊 不是查询 得向数据库里面完成插入的语句啊
huayiluo 2008-06-05
  • 打赏
  • 举报
回复
你可以这样做,
我把数据查出来,然后就马上关掉。不需要用户通知,再说了。我的连接可不是给用户管理的!
我只为用户提供数据。你查出了数据,就关掉。这样是浪费,没办法。
daocha 2008-06-05
  • 打赏
  • 举报
回复
那应该怎么办呢?
huayiluo 2008-06-05
  • 打赏
  • 举报
回复
关闭IE的时候

你又不能关IE然后通知服务器。你得把观念搞好。这是web程序!!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧