因为是向数据库中写图片了,所以上传的时候我先建了一个临时的文件夹叫TEMP <%@ page import="com.jspsmart.upload.SmartUpload" %> <%@ page import="com.jspsmart.upload.SmartUploadException" %> <%@ page import="com.gallop.util.*" %>
<META http-equiv="Content-Style-Type" content="text/css"> <LINK href="../theme/css.css" rel="stylesheet" type="text/css"> <script language="javascript" src="../include/hetong.js"></script> <style type="text/css"> <!-- .style1 {color: #FF0000} --> </style> <body> <jsp:useBean id="DBO" class="com.gallop.database.ConnectionManager" scope="page"/> <% try{ String URLpath=request.getRequestURL().toString(); String realPath = request.getRealPath(URLpath); int flag = realPath.indexOf("http:"); realPath = realPath.substring(0,flag); realPath = realPath + "Temp"; SmartUpload upLoad = new SmartUpload(); upLoad.initialize(config,request,response); upLoad.setMaxFileSize(20000000); upLoad.setAllowedFilesList("jpg"); upLoad.upload(); com.jspsmart.upload.File file = upLoad.getFiles().getFile(0); //get the upload file's name String fileName = file.getFileName(); //file path String filePath = realPath + "\\" + fileName; //save the upload file file.saveAs("/Temp/" + fileName); String name = upLoad.getRequest().getParameter("name"); java.io.File file1 = new java.io.File(filePath); InputStream in = new FileInputStream(file1); String C02ID=""; C02ID=upLoad.getRequest().getParameter("C02ID"); System.out.println(C02ID); try{ Connection conn=DBO.getConnection(); String sqlstr = "insert into C02FJ(C02ID,FJ) values (?,?)"; PreparedStatement stat = conn.prepareStatement(sqlstr); stat.setString(1,C02ID); stat.setBinaryStream(2,in,(int)file1.length()); int i = stat.executeUpdate(); in.close(); } catch(java.sql.SQLException ee){ out.print(ee.toString()); } boolean b = file1.delete(); if(b){ %> <center>文件上传成功</center> <p> </p> <p> </p> <p align="center"> </p> <p align="center"> </p> <form name="form1" method="post" action="bianhao.jsp"> <div align="center"> <input type="hidden" name="C02ID" value="<%=C02ID%>"> <input type="button" name="Submit" value="还要上传" onClick="fuwei()"> <input type="button" name="Submit" value="结束操作" onClick="closeWindow()"> </div> </form> <p align="center"> <% } else{ %> <center><h1 class="style1">对不起,文件上传失败了,请检查你以前的操作!!!</h1> </center> <div align="center"> <form name="form2" method="post" action=""> <input type="submit" name="Submit" value="返回上一步" onClick="fuwei()"> </form> </div> <% } } catch(Exception e){ e.printStackTrace(System.out); } %> </p>
</body> 你也看到了我使用的是jspsmart的一个上传组件