jsp上传文件到数据库和从数据库下载文件
用了commons-fileupload-1.2.jar和commons-io-1.3.2.jar这两个包。
save.jsp
<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>保存上传文件</title>
</head>
<body bgcolor='#FFFFFF' text='#000000' leftmargin='0' topmargin='40' marginwidth='0' marginheight='0'>
<font size='6' color='blue'>文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width='100%'>
<tr>
<td bgcolor=#008080>文件名</td>
<td bgcolor=#008080>大小</td>
</tr>
<%
// 连接字
String SQL_CON_DRIVER = "com.mysql.jdbc.Driver";
String SQL_CON_USERNAME = "root";
String SQL_CON_PASSWORD = "ddd";
String SQL_CON_STRING = "jdbc:mysql://localhost/ddd?useUniCode=true&characterEncoding=utf-8";
// 连接驱动
try{
Class.forName(SQL_CON_DRIVER).newInstance();
}
catch(ClassNotFoundException e){
out.print(e);
}
// 连接
Connection conn = DriverManager.getConnection(SQL_CON_STRING,SQL_CON_USERNAME,SQL_CON_PASSWORD);
ResultSet rs = null;
PreparedStatement pstmt=conn.prepareStatement("insert into require_file(file_name,file_content) values(?,?)");
%><%
int errorType_int =1;
String msg = "";
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节
//fu.setSizeMax(2*1024*1024);//2m
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//(临时存放目录,文件上传完毕后有办法清除它吗?)
// fu.setRepositoryPath("D:\\TEMP");
//开始读取上传信息
相关文档:
JS代码
<script
type="text/javascript">
function closewindow(){
window.returnValue = true;
window.close();
}
function clock(){
i = i -1;
if(document.getElementById("info")){
document.getElementById("info").innerHTML
......
JSP中结果集显示如下:
<
ww:iterator
value
=
"resultList"
status
=
"status"
>
<
tr
class
=
"<ww:if test="
#status.even"
>
row-even
</
ww:if
> ......
JSP/Servlet的编码原理
关键字: jsp, servlet编码
首先,说说 JSP/Servlet 中的几个编码的作用
在 JSP/Servlet 中主要有以下几种设置编码的方式:
pageEncoding ="UTF-8"
contentType = "text/html;charset=UTF-8"
request.setCharacterEncoding("UTF-8")
response.setCharacterEncodin ......
JSP中可能会碰到的问题解答
来源:不详 作者:佚名 更新时间: 2009年07月02日 点击:
JSP中可能会碰到的问题解答
1、如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含纯HTML:
< !--#include file="data.inc"-- >
但是如果data.inc中包含JSP CODE ,我们可以使用:
< %@include file= ......
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
<meta http-equiv="pragma" content="no-cache"&g ......