2个回答
展开全部
哥项目里面有,我改成大白话,你自己看看吧~!
SSH
1、在页面里面要有一个file
2、获取这个file,以blob形式存入数据库
3、读取blob输出到页面就可以了
<input type="file" 其他属性忽略 />
FormFile file0 = addForm.getSignfile();(这里用的是struts)
下面是用hibernate存数据,JDBC一样的用,去掉hibernate 而已
if(file0.getFileSize()!=0){
int titleSize=file0.getFileSize();
int t=10*1048576;
//if(titleSize>t){
//return mapping.findForward("f");
//}
Session s = HibernateUtil.getSessionFactory().openSession();
try {
s.beginTransaction();
String hql="From TsUserSign Where userid="+userid;
List list=s.createQuery(hql).list();
TsUserSign tsUserSign=new TsUserSign();
if (list.size()>0)
tsUserSign=(TsUserSign)list.get(0);
tsUserSign.setUserid(userid);
tsUserSign.setSignbody(Hibernate.createBlob(new byte[1]));
s.saveOrUpdate(tsUserSign);
s.flush();
s.refresh(tsUserSign, LockMode.UPGRADE);
SerializableBlob body = (SerializableBlob) tsUserSign.getSignbody();
Blob body1 = body.getWrappedBlob();
BLOB body2 = (BLOB) body1;
//OutputStream out = body2.getBinaryOutputStream();
OutputStream out =body2.setBinaryStream(1L);
InputStream is = file0.getInputStream();
int readSize = 0;
byte[] buffer = new byte[1024];
while ((readSize = is.read(buffer, 0, 1024)) != -1) {
out.write(buffer, 0, readSize);
}
is.close();
out.close();
s.saveOrUpdate(tsUserSign);
s.flush();
s.getTransaction().commit();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
s.getTransaction().rollback();
throw new Exception("手写签名文件保存失败!");
}
finally
{
s.close();
}
}
从Blob body1 = body.getWrappedBlob();这边开始,读与存其实都是一样的。。。你自己改改
SSH
1、在页面里面要有一个file
2、获取这个file,以blob形式存入数据库
3、读取blob输出到页面就可以了
<input type="file" 其他属性忽略 />
FormFile file0 = addForm.getSignfile();(这里用的是struts)
下面是用hibernate存数据,JDBC一样的用,去掉hibernate 而已
if(file0.getFileSize()!=0){
int titleSize=file0.getFileSize();
int t=10*1048576;
//if(titleSize>t){
//return mapping.findForward("f");
//}
Session s = HibernateUtil.getSessionFactory().openSession();
try {
s.beginTransaction();
String hql="From TsUserSign Where userid="+userid;
List list=s.createQuery(hql).list();
TsUserSign tsUserSign=new TsUserSign();
if (list.size()>0)
tsUserSign=(TsUserSign)list.get(0);
tsUserSign.setUserid(userid);
tsUserSign.setSignbody(Hibernate.createBlob(new byte[1]));
s.saveOrUpdate(tsUserSign);
s.flush();
s.refresh(tsUserSign, LockMode.UPGRADE);
SerializableBlob body = (SerializableBlob) tsUserSign.getSignbody();
Blob body1 = body.getWrappedBlob();
BLOB body2 = (BLOB) body1;
//OutputStream out = body2.getBinaryOutputStream();
OutputStream out =body2.setBinaryStream(1L);
InputStream is = file0.getInputStream();
int readSize = 0;
byte[] buffer = new byte[1024];
while ((readSize = is.read(buffer, 0, 1024)) != -1) {
out.write(buffer, 0, readSize);
}
is.close();
out.close();
s.saveOrUpdate(tsUserSign);
s.flush();
s.getTransaction().commit();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
s.getTransaction().rollback();
throw new Exception("手写签名文件保存失败!");
}
finally
{
s.close();
}
}
从Blob body1 = body.getWrappedBlob();这边开始,读与存其实都是一样的。。。你自己改改
展开全部
不知道你问的是哪个语言,给你提供一下 。net 方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class niunantest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = FileUpload1.PostedFile.ContentType;
Response.Write("文件类型:"+str);
string filename = "";
FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
if (FileValidation.IsAllowedExtension(FileUpload1, fe))
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
Response.Write("<br>验证通过!");
//filename = "/Images/" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
//FileUpload1.PostedFile.SaveAs(Server.MapPath(filename));
}
else
{
Response.Write( "<br>验证不通过,只支持以下格式的图片:JPG,GIF,PNG");
return;
}
}
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
SWF = 6787,
RAR = 8297,
ZIP = 8075,
_7Z = 55122
// 255216 jpg;
// 7173 gif;
// 6677 bmp,
// 13780 png;
// 6787 swf
// 7790 exe dll,
// 8297 rar
// 8075 zip
// 55122 7z
// 6063 xml
// 6033 html
// 239187 aspx
// 117115 cs
// 119105 js
// 102100 txt
// 255254 sql
}
public class FileValidation
{
public static bool IsAllowedExtension(FileUpload fu, FileExtension[] fileEx)
{
int fileLen = fu.PostedFile.ContentLength;
byte[] imgArray = new byte[fileLen];
fu.PostedFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
br.Close();
ms.Close();
foreach (FileExtension fe in fileEx)
{
if (Int32.Parse(fileclass) == (int)fe)
return true;
}
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class niunantest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = FileUpload1.PostedFile.ContentType;
Response.Write("文件类型:"+str);
string filename = "";
FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
if (FileValidation.IsAllowedExtension(FileUpload1, fe))
{
string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
Response.Write("<br>验证通过!");
//filename = "/Images/" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
//FileUpload1.PostedFile.SaveAs(Server.MapPath(filename));
}
else
{
Response.Write( "<br>验证不通过,只支持以下格式的图片:JPG,GIF,PNG");
return;
}
}
public enum FileExtension
{
JPG = 255216,
GIF = 7173,
PNG = 13780,
SWF = 6787,
RAR = 8297,
ZIP = 8075,
_7Z = 55122
// 255216 jpg;
// 7173 gif;
// 6677 bmp,
// 13780 png;
// 6787 swf
// 7790 exe dll,
// 8297 rar
// 8075 zip
// 55122 7z
// 6063 xml
// 6033 html
// 239187 aspx
// 117115 cs
// 119105 js
// 102100 txt
// 255254 sql
}
public class FileValidation
{
public static bool IsAllowedExtension(FileUpload fu, FileExtension[] fileEx)
{
int fileLen = fu.PostedFile.ContentLength;
byte[] imgArray = new byte[fileLen];
fu.PostedFile.InputStream.Read(imgArray, 0, fileLen);
MemoryStream ms = new MemoryStream(imgArray);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
string fileclass = "";
byte buffer;
try
{
buffer = br.ReadByte();
fileclass = buffer.ToString();
buffer = br.ReadByte();
fileclass += buffer.ToString();
}
catch
{
}
br.Close();
ms.Close();
foreach (FileExtension fe in fileEx)
{
if (Int32.Parse(fileclass) == (int)fe)
return true;
}
return false;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询