html:前台:简单的一个上传界面,可以浏览上传的文件,可以上传各种文件,前台没有验证,如过需要安全验证,可以在js中加验证
abc.jsp:
<form id="form1" name="form1" action="" method="post" >
<table style="display:none;" id="table6" width="100%" align="center" cellpadding="0" cellspacing="1">
<tr class="tableListContent">
<td width="10%">
<input type="file" id="f1" name="f1" style="position:absolute;filter:alpha(opacity=0);width:30px;" οnchange="imp()"/>
<input type="button" value="导入数据" Class="listLink" >
</td>
</tr>
</table>
</form>
js:
function imp(){
var form = document.getElementById("form1");
form.setAttribute('method','post');
if(form.encoding){
form.setAttribute('encoding','multipart/form-data');
}else{
form.setAttribute('enctype','multipart/form-data');
}
form.setAttribute('action','abc2.jsp);
form.submit();
}
abc2.jsp的内容:是在页面写的处理
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 10);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024 * 1024 * 10);
try {
List items = upload.parseRequest(request); // 得到所有FileItem
// 上传文件的个数
// 上传文件的个数
Iterator iter = items.iterator();
// 循环处理所有文件
int i=0;
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
// 判断是否是表单元素(<input type="text" />单选,多选等)
if (!item.isFormField()) {
// 得到文件的名称
name = item.getName();
// 文件长度
long size = item.getSize();// 过滤大小
if (name == null || "".equals(name.trim())) {
// 未选择上传文件
continue;
}
int end = name.length();
// 返回在此字符串中最右边出现的指定子字符串的索引。
int begin = name.lastIndexOf("\\");
//int start = name.lastIndexOf(".");
// 输出上传文件类型,此处可以进行类型的过滤
name=name.substring(begin+1);
InputStream in=item.getInputStream();
UpLoadBean upLoadBean = new UpLoadBean();
upLoadBean.saveOriginalData(in,name,size,flag,sampleid);
}
}
}catch (FileUploadException e) {
// 处理文件尺寸过大异常
e.printStackTrace();
} catch (Exception e) {
// 处理文件写入时的异常
e.printStackTrace();
}
}
response.sendRedirect("abc.jsp);//返回abc.jsp页面
上传的javaBean:UpLoadBean:
public class UpLoadBean {
private static Logger log = Logger.getLogger(SampleBean.class);
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
public void close_all(){
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void saveCurveUpload(InputStream blobfile,String filename,long size,Integer flag,Integer sampleid){//Integer flag,Integer sampleid是两个我自己项目中的参数不要
//的可以直接去掉
conn=DBUtil.getJNDIConnection();
String sql="insert into curveupLoad(blobfile,filename,flag,sampleid) " +
"values(?,?,?,?)";
log.info(sql);
try{
pstmt=conn.prepareStatement(sql);
pstmt.setBinaryStream(1, blobfile,(int)size);
pstmt.setString(2, filename);
pstmt.setInt(3, flag);
pstmt.setInt(4, sampleid);
pstmt.executeUpdate();
close_all();
}catch(SQLException e){
e.printStackTrace();
}
}
public void saveOriginalData(InputStream blobfile,String filename,long size,Integer flag,Integer sampleid){
conn=DBUtil.getJNDIConnection();
String sql="insert into Originaldata(blobfile,filename,flag,sampleid) " +
"values(?,?,?,?)";
log.info(sql);
try{
pstmt=conn.prepareStatement(sql);
pstmt.setBinaryStream(1, blobfile,(int)size);
pstmt.setString(2, filename);
pstmt.setInt(3, flag);
pstmt.setInt(4, sampleid);
pstmt.executeUpdate();
close_all();
}catch(SQLException e){
e.printStackTrace();
}
}
}
数据库:(是以2进制形式存储的,数据库用的是oracle)
字段 | |
Id | 主键 |
blobFILE | Blob文件 |
Flag | 标志 |
Sampleid | 外键 |