您的位置:首页 > Web前端 > JavaScript

jspsmartupload 详细实例(2)

2008-12-09 21:25 246 查看
上传:

<%@page contentType="text/html;charset=gb2312"%>
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<HTML>
<BODY BGCOLOR="white">

<H1>jspSmartUpload : Sample 4</H1>
<HR>

<%
// Connect to the database

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.4.105:1521:dbName";
//orcl为你的数据库的SID
String user="uNane";
String password="pass";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();

// Initialization
mySmartUpload.initialize(pageContext);

// Upload
mySmartUpload.upload();

// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){

try {
String sFileName = mySmartUpload.getFiles().getFile(0).getFileName() ;
mySmartUpload.save("/upload");
String sPath = request.getRealPath("/upload/")+sFileName ;
java.io.File file = new java.io.File(sPath);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
PreparedStatement ps =
conn.prepareStatement("insert into tfile values (?,?,?)");
ps.setInt(1,3);
ps.setString(2,file.getName());
ps.setBinaryStream(3,fis,(int)file.length());
ps.executeUpdate();
ps.close();
fis.close();

} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}

}

stmt.close();
conn.close();

%>
</BODY></HTML>

读取:

<%@ page language="java" import="java.sql.*,java.util.*"%>
<%
String image_id ="2" ;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.4.105:1521:dbName";
//orcl为你的数据库的SID
String user="uName";
String password="pass";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM tfile WHERE id = " + image_id);
if (rs.next())
{
String dim_image = rs.getString("filename");
byte [] blocco = rs.getBytes("tfile");
response.setContentType("image/jpg");
javax.servlet.ServletOutputStream op = response.getOutputStream();
for(int i=0;i<blocco.length;i++)
{
op.write(blocco[i]);
}
}
op.close() ;
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
}
}
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: