java 文件上传格式,javaweb中的文件上传(以2进制格式上传)

html:前台:简单的一个上传界面,可以浏览上传的文件,可以上传各种文件,前台没有验证,如过需要安全验证,可以在js中加验证

abc.jsp:

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();

// 判断是否是表单元素(单选,多选等)

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

外键

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值