日期:2014-05-17  浏览次数:20672 次

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

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();