利用commons-fileupload-1.2.2上传…

来源:互联网 发布:滴滴截图造假软件 编辑:程序博客网 时间:2024/06/06 08:44

前台页面:

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN">
<html>
  <head>
   <basehref="<%=basePath%>">
   
   <title>My JSP 'uploadFile.jsp'starting page</title>
   
 <meta http-equiv="pragma"content="no-cache">
 <meta http-equiv="cache-control"content="no-cache">
 <meta http-equiv="expires"content="0">   
 <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
 <meta http-equiv="description"content="This is my page">
 <!--
 <link rel="stylesheet"type="text/css" href="styles.css">
 -->

 </head>
 
  <body>
   <div align="center">
 <p>请选择要上传的文件</p>
  <formaction="${pageContext.request.contextPath}/servlet/UploadServlet"method="post" enctype="multipart/form-data" name="form1"id="form1">
   <table width="408" height="108"border="1">
     <tr>
       <td>上传文件:</td>
       <td><label>
         <input name="uploadfile" type="file" id="uploadfile"/>
       </label></td>
     </tr>
     <tr>
       <td><label>
         <input type="submit" name="Submit" value="提交"/>
       </label></td>
       <td>&nbsp;</td>
     </tr>
   </table>
  </form>
 <p>&nbsp; </p>
</div>
  </body>
</html>

后台上传代码:

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.fileupload.FileItem;
importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
importorg.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet{

 private static final longserialVersionUID = 3144946693290762048L;

 public void doGet(HttpServletRequestrequest, HttpServletResponse response)
   throwsServletException, IOException {
  request.setCharacterEncoding("UTF-8");

  DiskFileItemFactoryfactory = new DiskFileItemFactory();
  ServletFileUpload upload = newServletFileUpload(factory);

  try {
   List<?>items = upload.parseRequest(request);
   Iterator<?>it = items.iterator();
   while(it.hasNext()) {
    FileItemitem = (FileItem) it.next();
    if(!item.isFormField()) {

     if(item.getName() != null &&!item.getName().equals("")) {
      QueryRunnerqr = DbHelper.getQueryRunner();
      Stringsql = "insert into photo (path) values (?)";
      qr.update(sql,item.getName());
      request.setAttribute("upload.message","上传文件成功!");

     }else {
      request.setAttribute("upload.message","没有选择上传文件!");
     }
    }
   }

  } catch (Exceptione) {
   e.printStackTrace();
   request.setAttribute("upload.message","上传文件不成功!");
  }
  request.getRequestDispatcher("/uploadResult.jsp").forward(request,
    response);
 }

 public void doPost(HttpServletRequestrequest, HttpServletResponse response)
   throwsServletException, IOException {

  doGet(request,response);
 }
}

从数据库中查询的代码

 

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
importorg.apache.commons.dbutils.handlers.BeanListHandler;

public class PrintServlet extends HttpServlet{

 public void doGet(HttpServletRequestrequest, HttpServletResponse response)
   throwsServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  QueryRunnerqr=DbHelper.getQueryRunner();
  String sql="select path fromphoto";
  List list=null;
  photo photo=null;
  String path=null;
  try {
   list=(List)qr.query(sql,new BeanListHandler(photo.class));
   photo=(photo)list.get(0);
   path=photo.getPath();
  } catch (SQLException e){
   e.printStackTrace();
  }
  request.setAttribute("path",path);
  request.getRequestDispatcher("/Result.jsp").forward(request,response);
 }

 public void doPost(HttpServletRequestrequest, HttpServletResponse response)
   throwsServletException, IOException {

  doGet(request,response);
 }

}
前台页面显示:

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="zhao.ai.nan.upload.photo"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN">
<html>
  <head>
   <basehref="<%=basePath%>">
   
   <title>My JSP 'Result.jsp' startingpage</title>
   
 <meta http-equiv="pragma"content="no-cache">
 <meta http-equiv="cache-control"content="no-cache">
 <meta http-equiv="expires"content="0">   
 <meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
 <meta http-equiv="description"content="This is my page">
 <!--
 <link rel="stylesheet"type="text/css" href="styles.css">
 -->

 </head>
 
  <body>
   <% Stringp=(String)request.getAttribute("path");
   %>
   <img src="<%=p %>"alt="" />
  </body>
</html>
其中有两个帮助类

public class photo {
 private String path;

 public String getPath() {
  return path;
 }

 public void setPath(String path){
  this.path = path;
 }
 
}

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

importorg.apache.commons.dbutils.QueryRunner;

public class DbHelper {
 public static QueryRunner getQueryRunner(){
  DataSource ds=null;
  try {
   Contextcontext=new InitialContext();
   ds=(DataSource)context.lookup("java:/comp/env/jdbc/SQLServerds");
  } catch (NamingException e){
   e.printStackTrace();
  }
  QueryRunner qr=newQueryRunner(ds);
  return qr;
 }
}

0 0
原创粉丝点击