读书人

怎样下传图片到服务器数据库

发布时间: 2012-11-08 08:48:11 作者: rapoo

怎样上传图片到服务器数据库?
我的代码是

1.test.html
<html>
<head>
<tile>
image
</tile>
</head>
<body>
<form method=post action="testimage.jsp">
<input type="text" name="content">
<input type="file" name="image">
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTMl>


2.testimage。jsp


<%@ page language="java" contentType="text/html;charset=GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>


<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>


<jsp:useBean class="firm.firm" id="s_checkItem" scope="page"></jsp:useBean>


<%

String content =request.getParameter("content");

String filename=request.getParameter("image");


String path = request.getContextPath();
FileInputStream str=new FileInputStream(path+filename);

String sql="insert into image(content,image) values('"+content+"','"+filename+"')";


s_checkItem.executeUpdate(sql);

%>


HTTP Status 404 - http_imgload[9].jpg (系统找不到指定的文件。)

--------------------------------------------

type Status report

message http_imgload[9].jpg (系统找不到指定的文件。)

description The requested resource (http_imgload[9].jpg (系统找不到指定的文件。)) is not available.


我的图片路径是:C:\http_imgload[9].jpg

怎么才能找到图片的绝对路径呢

[解决办法]
<%@ page language="java" import="java.util.*,shop.car.jdbc.*"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<%@ page import="java.sql.*"%>
<%@ page import="com.jspsmart.upload.*"%>
<%
//实例化上载bean
SmartUpload mySmartUpload = new SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024 * 1024);
//设定允许上传的文件(通过扩展名限制)
mySmartUpload.setAllowedFilesList("jpg");
//上载文件
mySmartUpload.upload();

int count = mySmartUpload.save("/upload",SmartUpload.SAVE_VIRTUAL);

out.println(count + "个文件上传成功!<br>");
out.println("TEST=" + mySmartUpload.getRequest().getParameter("TEST")
+ "<BR><BR>");

//循环取得所有上载的文件
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();
//取得文件的大小
int fileSize = myFile.getSize();
//保存路径
String aa = request.getRealPath(request.getServletPath())+myFileName;


String trace = aa + myFileName;
//取得别的参数
String explain=(String)mySmartUpload.getRequest().getParameter("text");
String send=(String)mySmartUpload.getRequest().getParameter("send");
//将文件保存在服务器端
myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
//下面的是将上载的文件保存到数据库中
//将文件读到流中
java.io.File file = new java.io.File(trace);
java.io.FileInputStream fis = new java.io.FileInputStream(
file);
out.println(file.length());
//打开数据库
ResultSet rs = null;
String sql = null;
PreparedStatement pstm = null;
Connection conn = null;
try {
conn = DB.getConnection();
//将文件写到数据库中
sql = "insert into goods (viewpic) values (?)";
pstm = conn.prepareStatement(sql);
pstm.setBinaryStream(1, fis, (int) file.length());
pstm.executeUpdate();
out.println(("上载成功!!!").toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
out.println(("上载失败!!!").toString());
}
}//与前面的if对应
%>
</body>
</html>


读书人网 >Java Web开发

热点推荐