📄 sendpic.jsp
字号:
<%-- sendPic.jsp程序代码 --%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="com.jspsmart.upload.SmartUpload" %>
<%@ page import="com.jspsmart.upload.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%
// 创建一个SmartUpload对象
SmartUpload smartUpload = new SmartUpload();
//上传初始化
smartUpload.initialize(pageContext);
//限制上传文件的最大长度。
smartUpload.setMaxFileSize(10000);
//限制总上传数据的长度。
smartUpload.setTotalMaxFileSize(15000);
//设置允许上传的文件(通过扩展名限制),仅允许gif、jpg文件。
smartUpload.setAllowedFilesList("GIF,gif,JPG,jpg");
//接收上传文件数据
smartUpload.upload();
//将上传文件保存到指定目录
smartUpload.save("/tmp_upload");
//取得页面传递过来的数据
request.setCharacterEncoding("gb2312");
String strTitle=smartUpload.getRequest().getParameter("picTitle");
String strInfo=smartUpload.getRequest().getParameter("picInfo");
//创建一个上传上来的图片文件的文件输出流,准备插入图片数据到数据库。
File picFile = smartUpload.getFiles().getFile(0); //第1个文件编号为0
String appRoot=application.getRealPath("/"); //获取当前Web站点的绝对路径
String picPath=appRoot+"/tmp_upload/"+picFile.getFileName();
FileInputStream picInputStream=new FileInputStream(picPath);
int picLength=picFile.getSize();
//System.out.println(picPath);
//建立数据库连接并执行数据插入
Connection conn=null;
PreparedStatement pstmt=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:datasource_suit","sa","710318");
String strSQL="insert into t_picture (title,info,picture) values (?,?,?)";
pstmt=conn.prepareStatement(strSQL);
pstmt.setString(1,strTitle);
pstmt.setString(2,strInfo);
pstmt.setBinaryStream(3,picInputStream,picLength);
pstmt.executeUpdate();
}catch(Exception e){
out.println("插入数据失败."+e.toString());
}
finally{
try{
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
if(picInputStream!=null){
picInputStream.close();
}
//删除服务器上临时生成的图片文件
java.io.File tmpFile=new java.io.File(picPath);
tmpFile.delete();
}catch(Exception e){}
}
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -