前台页面:
<%@ 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 HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=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>
<form action="${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> </td>
</tr>
</table>
</form>
<p> </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;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 3144946693290762048L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<?> items = upload.parseRequest(request);
Iterator<?> it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
if (item.getName() != null && !item.getName().equals("")) {
QueryRunner qr = DbHelper.getQueryRunner();
String sql = "insert into photo (path) values (?)";
qr.update(sql, item.getName());
request.setAttribute("upload.message", "上传文件成功!");
} else {
request.setAttribute("upload.message", "没有选择上传文件!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件不成功!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request,
response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, 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;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class PrintServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
QueryRunner qr=DbHelper.getQueryRunner();
String sql="select path from photo";
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(HttpServletRequest request, HttpServletResponse response)
throws ServletException, 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 HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Result.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>
<% String p=(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;
import org.apache.commons.dbutils.QueryRunner;
public class DbHelper {
public static QueryRunner getQueryRunner(){
DataSource ds=null;
try {
Context context=new InitialContext();
ds=(DataSource)context.lookup("java:/comp/env/jdbc/SQLServerds");
} catch (NamingException e) {
e.printStackTrace();
}
QueryRunner qr=new QueryRunner(ds);
return qr;
}
}