JSP文件上传

<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%@page import="utils.ImageFileUpload"%>

<%
	String msg = "";
	ResultSet rs = null;
	ImageFileUpload ifu = new ImageFileUpload();
	
		try {
			if (ServletFileUpload.isMultipartContent(request)) {
				DiskFileItemFactory dfifactory = new DiskFileItemFactory();
				// 设置最多只允许在内存中存储的数据
				dfifactory.setSizeThreshold(100 * 1204);
				// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
				dfifactory.setRepository(new File("C:\\Java\\temp"));

				ServletFileUpload sfu = new ServletFileUpload(dfifactory);

				List<FileItem> list = sfu.parseRequest(request);
				for (FileItem fi : list) {
					if (!fi.isFormField()) {
						if (fi.getSize() != 0) {
							if (fi.getSize() > (400 * 1024)) {
								msg = "文件大小不能超过400kb";
							} else {
								File f = new File(fi.getName());
								// 设置上传文件的目录
								String destSavePath = getServletContext()
										.getRealPath(getServletInfo())
										+ "\\images\\" + f.getName();

								// 将文件名保存至数据库
								if (ifu.saveFilename(f.getName())) {
									// 将文件写如服务器目录
									if (ifu.saveImage(fi, destSavePath)) {
										msg = "文件上传成功";
									}
								}
							}
						}
					}
					fi.delete();
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		
	// 显示当前数据库中的图片信息
	rs = ifu.getAll();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>文件上传演示</title>
	</head>

	<body>
		<h1>
			文件上传演示
		</h1>
		<form action="index.jsp" method="post" enctype="multipart/form-data">
			<input type="text" name="name" value="abc">
			<br>
			<input type="file" name="imagename">
			<br>
			<input type="submit" name="addImage" value="提交">
		</form>
		<%=msg%>
		<hr>
		<table border="1">
			<tr>
				<td>id</td>
				<td>Image</td>
			</tr>
			<%
				if (rs != null){
					while(rs.next()){
					%>
				<tr>
					<td><%=rs.getString(1)%></td>
					<td><img src="images/<%=rs.getString(2)%>" width="70"></td>
				</tr>
					<%
					}
				}
			%>
		</table>
	</body>
</html>


CREATE TABLE [dbo].[image](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[image] [varchar](50) NULL,
 CONSTRAINT [PK_image] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]



package utils;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.fileupload.FileItem;

public class ImageFileUpload {
	private Connection conn = null;

	public boolean saveFilename(String filename) {
		boolean flag = false;
		try {
			if (conn == null)
				conn = getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("insert into image values(?)");
			pstmt.setString(1, filename);
			pstmt.execute();
			flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return flag;
	}

	public boolean saveImage(FileItem item, String path) {
		boolean flag = false;
		try {
			item.write(new File(path));
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	private Connection getConnection() {
		try {
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:jtds:sqlserver://localhost:1433/test", "sa", "");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public ResultSet getAll() {
		ResultSet rs = null;
		try {
			if (conn == null)
				conn = getConnection();
			PreparedStatement pstmt = conn
					.prepareStatement("select * from image");
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

}


阅读更多
个人分类: Java技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭