日期:2014-05-16 浏览次数:20505 次
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> &