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) {
}
}
%>
<%@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) {
}
}
%>
相关文章推荐
- jspsmartupload 详细实例(1)
- 解决JSPSMARTUPLOAD下载文件中文乱码(有实例)
- jspSmartUpload 上传下载实例
- 使用smartupload组件实现jsp+jdbc上传下载文件实例解析
- 用JSP smartupload 上次图片简单实例
- 基于jspSmartUpload的Jsp文件上传实例
- jspSmartUpload讲解的很详细
- 在jsp中使用smartupload组件上传文件
- jsp实现文件上传(一)用jspSmartUpload组件实现文件上传
- jspSmartUpload上传下载全攻略
- 怎样用JspSmartUpload实现文件上传攻略
- jspSmartUpload.jar下载 转:虚竹之屋
- jsp文件上传开源项目jspSmartUpload的应用
- jsp SmartUpload上传和下载代码全攻略
- 真正解决jspSmartUpload组件上传下载文件时中文乱码问题!!!
- jspSmartUpload上传下载全攻略
- jspSmartUpload上传下载全攻略
- JspSmartUpload上传中文时出现乱码的解决方法
- jspSmartUpload组件中的主要类
- jspSmartUpload实现上传现在