jsp初学者请教各位大虾关于文件保存到blob中,100分!!!
我是从asp转过来的,感觉到jsp确实比asp麻烦很多。我在asp中改写稻香老农的伍组建上传很容易就能实现上传直接保存到oracle的blob中,可是在jsp中我改写jspsmartupload就没那么轻松了。代码如下:
在XX.jsp中
String query = "INSERT INTO T_DOCTYPE (TYPESORT,TYPEID,TYPENAME,CONTENT,USERID)VALUES ( ' " +TYPESORT+ " ', ' "+ docid + " ', ' " + TYPENAME + " ', EMPTY_BLOB(), ' " + userID + " ') ";
java.sql.PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.executeUpdate();
String sql = "update T_DOCTYPE set CONTENT where WHERE TYPEID= ' " + docid + " ' " ;
pstmt = conn.prepareStatement(sql);
//query = "SELECT CONTENT FROM T_DOCTYPE WHERE TYPEID= ' " + docid + " ' FOR UPDATE ";
//java.sql.Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//ResultSet rs = stmt.executeQuery(query);
try{
myFile.fileToBlob(pstmt);
}catch(Exception ex){
ex.printStackTrace();
}
smartupload的file.java中增加
public void fileToBlob(java.sql.PreparedStatement rs)
throws SQLException, SmartUploadException, IOException, ServletException
{
long numBlocks = 0L;
int blockSize = 0x10000;
int leftOver = 0;
int pos = 0;
if(rs == null)
throw new IllegalArgumentException( "The RecordSet cannot be null (1145). ");
if(columnName == null)
throw new IllegalArgumentException( "The columnName cannot be null (1150). ");
if(columnName.length() == 0)
throw new IllegalArgumentException( "The columnName cannot be empty (1155). ");
numBlocks = BigInteger.valueOf(m_size).divide(BigInteger.valueOf(blockSize)).longValue();
leftOver = BigInteger.valueOf(m_size).mod(BigInteger.valueOf(blockSize)).intValue();
try
{
for(int i = 1; (long)i < numBlocks; i++)
{
rs.setBinaryStream(1, new ByteArrayInputStream(m_parent.m_binArray, pos, blockSize), blockSize);
//rs.updateBinaryStream(columnName, new ByteArrayInputStream(m_parent.m_binArray, pos, blockSize), blockSize);
pos = pos != 0 ? pos : 1;
pos = i * blockSize;
}
if(leftOver > 0)
rs.setBinaryStream(1, new ByteArrayInputStream(m_parent.m_binArray, pos, leftOver), leftOver);
//rs.updateBinaryStream(columnName, new ByteArrayInputStream(m_parent.m_binArray, pos, leftOver), leftOver);
rs.execute();
}
catch(SQLException e)
{
byte binByte2[] = new byte[m_size];
System.arraycopy(m_parent.m_binArray, m_startData, binByte2, 0, m_size);
//rs.updateBytes(columnName, binByte2);
}
catch(Exception e)
{
throw new SmartUploadException( "Unable to save file in the DataBase (1130). ");
}
}
没抱错,反正没成功,不知道怎么回事
[解决办法]
路过,是客户端上传文件吗?
[解决办法]
给你个JSP上传ORACLE成功的
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page language= "java "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "com.dd.database.DatabaseService "%>
<br>
<%
try
{
String sessionID=session.getId();
DatabaseService pobjDatabaseService = new DatabaseService();
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i <mySmartUpload.getFiles().getCount();i++)
{
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得文件全名
String myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf( '. '));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
if(ext.equals( "exe "))
{
out.println( "上传文件不能为EXE文件 ");
return;
}
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath( "/ ")+ "upfile\\ ";
String trace=aa+myFileName;
//取得别的参数
String explain=(String)mySmartUpload.getRequest().getParameter( "text ");
String send=(String)mySmartUpload.getRequest().getParameter( "send ");
//将文件保存在服务器端
myFile.saveAs(trace,com.jspsmart.upload.File.SAVEAS_PHYSICAL);
//将上载的文件保存到数据库中
//将文件读到流中
java.io.File file = new java.io.File(trace);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
//打开数据库
String mStrSql=null;
ResultSet mobjResult=null;
PreparedStatement mobjPrestmt=null;
mStrSql= "insert into H_uploadfile(markname,marksize,MarkBody,randomname,sessionid) values (?,?,?,?,?) ";
java.util.Random pobjRandom = new java.util.Random(new java.util.Date().getTime());
long plogRandom = pobjRandom.nextLong();
String mstrRandomName = String.valueOf(plogRandom);
//随机名
mstrRandomName = mstrRandomName + ". " + ext;
mobjPrestmt=pobjDatabaseService.getPreparedStatement(mStrSql);
mobjPrestmt.setString(1, suffix);
mobjPrestmt.setInt(2, fileSize);
mobjPrestmt.setBinaryStream(3,fis,(int)file.length());
mobjPrestmt.setString(4,mstrRandomName);
mobjPrestmt.setString(5,sessionID);
pobjDatabaseService.setAutoCommit();
mobjPrestmt.executeUpdate();
pobjDatabaseService.commit();
response.sendRedirect( "jlwj/uploadwenjian.jsp?id=15 ");
}
else
{
out.println(( "上载失败! ").toString());
}
}
}
catch(Exception er)
{
out.println( " <br> ");
out.println(er.getMessage());
}
%>
[解决办法]
可以用Apache的FileUpLoad组件,非常好用,而且是开源的,你可以参考一下
------解决方案--------------------
不会不报错又不成功的。
你在catch里增加写debug信息试试。
你的代码在这里显示得有点乱,看的不是很清楚。