实现图片文件上传到数据库
我用的数据库是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(){
StringBuffer sb=new StringBuffer();
try{
Upload uploader = new Upload(session.getServletContext(), request,JPath.getAppPath()+ "uploadfile/ ", "GBK ", 500000);
uploader.setSaveInTempFile(false);
UploadMsg upMsg = uploader.save();
/*
* 如果上传成功,做相应处理
*/
if (upMsg.isSuccessful)
{
Map <String,String> paras=uploader.getOtherParameters();
String title=paras.get( "title ");
String introduce =paras.get( "introduce ");
String filename=((UploadedFile) uploader.getUploadedFiles().get(0)).getFileName_Saved();
String smallpic =filename;
Stringbigpic =((UploadedFile) uploader.getUploadedFiles().get(0)).getAbsoluteFileName_Saved();
try {
dao = factory.createDAO();
WProduct vo = new WProduct();
dao.initAutoincreasingPrimaryKey( "w_product ");
vo.setMsgid(new Integer(dao.genAutoincreasingPrimaryKey( "w_product ").intValue()));
vo.setTitle(title);
vo.setIntroduce(introduce);
vo.setSmallpic(smallpic);
vo.setBigpic(bigpic);
dao.insert(vo);
dao.close();
}catch (Exception e) {
e.printStackTrace();
jlog.Error( " " + e);
}
this.resultString = " <script> alert( '上传成功 ');history.back(); </script> ";
}
else
{
//上传失败
}
}catch(Exception e){
e.printStackTrace();
}
}
[解决办法]
用smartupload吧
我的异常网推荐解决方案:org.apache.jasper.JasperException: Unable to compile class,http://www.myexception.cn/j2ee/2308.html