用fileupload组件上传时,有些文件不能导入数据库中,图片格式的,exe,rar中有图片也不能,文件扩展名改也不能上传,大家看看是不是我的程序文件有问题,还是有可能我的mysql有这方限制
upload.html
<html>
<head><title>文件上传</title></head>
<body>
<form action="upload2.jsp" method="POST" enctype="Multipart/form-data">
<table>
<tr>
<td>请选择要上传的文件:</td>
<td><input type="file" name="file1" size="40"></td>
</tr>
<tr>
<td>请输入文的描述:</td>
<td><input type="text" name="desc1" size="40"></td>
</tr>
<!--<tr>
<td>请选择要上传的文件:</td>
<td><input type="file" name="file2" size="50"></td>
</tr>
<tr>
<td>请输入文件的描述:</td>
<td><input type="text" name="desc2" size="40"><td>
</tr>-->
<tr>
<td><input type="reset" value="重填"></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>upload2.jsp
<%@ 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"%><html>
<head><title>upload2</title></head>
<body>
<%
DiskFileUpload dfu=new DiskFileUpload();
//设置上传数据的最大大小为10M。
dfu.setSizeMax(0xA00000); //设置内存缓冲区的阀值为512K。
dfu.setSizeThreshold(0x80000); //设置临时存储文件的目录为E:\fileupload。
dfu.setRepositoryPath("d:\\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=ds.getConnection();
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>
upload.html
<html>
<head><title>文件上传</title></head>
<body>
<form action="upload2.jsp" method="POST" enctype="Multipart/form-data">
<table>
<tr>
<td>请选择要上传的文件:</td>
<td><input type="file" name="file1" size="40"></td>
</tr>
<tr>
<td>请输入文的描述:</td>
<td><input type="text" name="desc1" size="40"></td>
</tr>
<!--<tr>
<td>请选择要上传的文件:</td>
<td><input type="file" name="file2" size="50"></td>
</tr>
<tr>
<td>请输入文件的描述:</td>
<td><input type="text" name="desc2" size="40"><td>
</tr>-->
<tr>
<td><input type="reset" value="重填"></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>upload2.jsp
<%@ 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"%><html>
<head><title>upload2</title></head>
<body>
<%
DiskFileUpload dfu=new DiskFileUpload();
//设置上传数据的最大大小为10M。
dfu.setSizeMax(0xA00000); //设置内存缓冲区的阀值为512K。
dfu.setSizeThreshold(0x80000); //设置临时存储文件的目录为E:\fileupload。
dfu.setRepositoryPath("d:\\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=ds.getConnection();
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>
JAVA世纪网:Java面试题,JAVA面试技巧,JAVA自测题,Java代码,Java项目,Java学习资料
老紫竹的家:项目管理,数据库面试题,智力面试题,相声笑话,儿歌动画片