首先,能上传900多K的文件1M以上的文件上传不了,我也试着修改限制文件上传的大小,但是设置完了,还是有错误,代码和错误如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.List,java.util.Iterator"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="javax.naming.*,java.sql.*,javax.sql.DataSource"%>
<%@ page import="java.io.File"%>
<%@ page import="com.bjsxt.bbs.DB"%>
<html>
<head><title>upload2</title></head>
<body>
<%
DiskFileUpload dfu=new DiskFileUpload();
//设置上传数据的最大大小为10M。
dfu.setSizeMax(1024*1024*1000);
//限制文件上传最大是50M
dfu.setFileSizeMax(1024*1024*50);
dfu.setSizeThreshold(0x80000);
//设置临时存储文件的目录为E:\fileupload。
dfu.setRepositoryPath("E:\\fileupload");
//得到FileItem对象的列表。
List fileItems=dfu.parseRequest(request);
Iterator it = fileItems.iterator();
//Context ctx=new InitialContext();
//DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
Connection conn=DB.getConn();
PreparedStatement pstmt=conn.prepareStatement("insert into uploadfile(filename,filesize,data) values(?,?,?)");
//依次处理每个上传的文件
while (it.hasNext())
{
FileItem item = (FileItem) it.next();
//判断是否是文件域的表单信息
if (!item.isFormField())
{
String name = item.getName();
//如果浏览器传送的文件名是全路径名,则取出文件名。
int index=name.lastIndexOf(File.separator);
if(index>0)
name=name.substring(index+1,name.length());
long size = item.getSize();
if((name==null || name.equals("")) && size==0)
continue;
pstmt.setString(1,name);
pstmt.setInt(2,(int)size);
pstmt.setBinaryStream(3,item.getInputStream(),(int)size);
pstmt.executeUpdate();
}
}
if(pstmt!=null)
{
pstmt.close();
pstmt=null;
}
if(conn!=null)
{
conn.close();
conn=null;
}
out.println("上传成功!");
%>
</body>
</html>
错误提示是:
严重: Servlet.service() for servlet [jsp] in context with path [/load] threw exception [javax.servlet.ServletException: java.sql.SQLException: Parameter of prepared statement which is set through mysql_send_long_data() is longer than 'max_long_data_size' bytes] with root cause