实现图片文件上传到数据库
我用的数据库是SQL Server 2000 我想实现数据库保存图片路径 图片上传到服务器指定的文件夹中
请哪位好心的高手哥哥,高手姐姐给小第一个解决方案的源代码吧.我感激不尽!
E-mail;yaoyuan0903@hotmail.com
谢谢帮忙啊!!
------解决方案--------------------已发,我的邮箱:aimer311@sina.com,
------解决方案--------------------呵呵,楼上性急啊!
------解决方案--------------------JSp页面:
<%@page contentType= "text/html;charset=GBK "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-logic "
prefix= "logic "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-html "
prefix= "html "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-bean "
prefix= "bean "%>
<html>
<script type= "text/javascript ">
function v(){
document.myform.img.src=document.myform.pic.value;
}
</script>
<body>
<form action= "photo.do?p=main " method= "POST " enctype= "multipart/form-data " name= "myform ">
<input type= "file " name= "pic " onchange= "v() " >
<img name= "img " src= " " >
<img name= "abc " src= "photo.do?p=view ">
<input type= "submit " value= "going "/>
</form>
</body>
</html>
Action:
PhotoForm tform = (PhotoForm) form;
FormFile file = tform.getPic();
String name = "ruby ";
if (file.getFileSize() > 10000) {
}
// 获取文件名
String filename = file.getFileName();
int index = filename.lastIndexOf( ". ");
filename = filename.substring(index);
// 设置图片文件临时存放的路径
String path = "D:/ " + name + filename;
try {
// 读取文件中的数据,获取二进制的数据流
InputStream stream = file.getInputStream();
// 把数据写到指定路径
OutputStream bos = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
bos.close();
stream.close();
} catch (Exception fe) {
}
file.destroy();
然后就是得到文件路径的名称插入到数据库中.
------解决方案--------------------给我E_mail的哪位
这个错误是怎么回事 请指教 谢谢
exception
org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 5 in the jsp file: /uploadPhotoForm.jsp
Generated servlet error:
UserInfo
cannot be resolved to a type
An error occurred at line: 5 in the jsp file: /uploadPhotoForm.jsp
Generated servlet error:
UserInfo cannot be resolved to a type
An error occurred at line: 5 in the jsp file: /uploadPhotoForm.jsp
Generated servlet error:
UserInfo.BINDNAME cannot be resolved to a type
------解决方案--------------------有上传下载包smartupload
你可以把图片上传到一个文件夹,在数据库的表中,保留这个图片在网站上的相对路径,每次读相对地址,就可以把图片读出来。很方便的,我很早就已经实现了。
------解决方案--------------------private void uploadPic(){
Str