使用SmartUpload上传图片至MySQL数据库中的代码怎么写
我在网上找不到这方面的代码,自己编写的又传不上去.
谁有可以贴出来吗?
谢谢!!!
我在网上找不到这方面的代码,自己编写的又传不上去.
谁有可以贴出来吗?
谢谢!!!
解决方案 »
- 在jsp中使用jdom.jar包来生成一个文件,网页运行正常,但是没生成xml文件
- 高手救救我吧 !!Coparator的问题
- 请问一下,web迅雷,点网页关闭按钮时,最小化到右下交任务栏是怎么实现的?
- ewebeditor的使用问题?
- 在线等!如何能让字符串中“\”字符不会丢失?
- 请大家帮忙给看看好吗?如何解决下面程序的空指针异常?
- 奇怪的问题。大家进来帮我看看
- 在jsp中怎么样加密用户的注册密码并存入access数据? 又是如何取出,并验证用户登陆的密码? 急………………!!最好有代码!!
- 救命,给一个网站软硬件方案,五六十万左右
- 帮忙改改错,关于一个购物车的例子
- 关于 java applet的初级问题
- 请教如何用JSP做一个九九乘法表【javascript的我会做】
可当我读取到页面的时候全是乱码.
我到数据库里看了,也有东西啊!SmartUpload su = new SmartUpload();
su.initialize(pageContext);su.upload(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:/uploadpic/upload.mdb";
Connection con=DriverManager.getConnection(url) ;
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from smap where id=11");
while(rs.next())
{
rs.updateString("map_name",su.getFiles().getFile(0).getFileName());
su.getFiles().getFile(0).fileToField(rs,"map");
rs.updateRow();
}
那用SmartUpload组件,这句语句可以吗?
SmartUpload.getFiles().getFile(0).fileToField(rs,"map");
最好谁有完整的代码,给我发一份,不胜感激.
一个是中文名的问题 还有一个上传大文件时的问题
public class Upfile_Photo implements Upfile { PageContext pageContext = null;
HttpServletRequest request = null;
private String msg = "";
private boolean FoundErr = false;
private boolean EnableUpload = false;
private String SaveUpFilesPath="UploadThumbs";
private String SaveUpFilesPath2="UploadPhotos";
private String SavePath = "";
private int PhotoUrlID = 0;
public Upfile_Photo(PageContext pageContext, HttpServletRequest request, int PhotoUrlID) {
this.pageContext = pageContext;
this.request = request;
this.PhotoUrlID = PhotoUrlID;
}
public void upload() throws ServletException, SmartUploadException, IOException {
if(PhotoUrlID>0)
SavePath = "/"+SaveUpFilesPath2+"/";
else
SavePath = "/"+SaveUpFilesPath+"/";
SmartUpload myupload = new SmartUpload();
myupload.initialize(pageContext);//初始化
myupload.upload();
File file = myupload.getFiles().getFile(0);
if(file.getSize()<=0){
msg = "请先选择你要上传的文件!";
FoundErr = true;
}else if(file.getSize()>2*1024*1024){
msg = "文件大小超过了限制,最大只能上传 2MB 的文件!";
FoundErr = true;
}
String fileExt = file.getFileExt().toLowerCase();
String[] arrUpFileType = UpFileType.split(",");
for(int i=0; i<arrUpFileType.length; i++){
if(fileExt.equalsIgnoreCase(arrUpFileType[i])){
EnableUpload=true;
break;
}
}
if(fileExt.equalsIgnoreCase("jsp"))
EnableUpload=false;
if(!EnableUpload){
msg="这种文件类型不允许上传!只允许上传这几种文件类型:" + UpFileType;
FoundErr=true;
}
StringBuffer buf = new StringBuffer();
buf.append("<SCRIPT language=javascript>");
if(!FoundErr){
String filetype = "";
String fromfilename = "";
String filename = "";
if(file != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
fromfilename = file.getFileName();
if (!(fromfilename.equals(""))) {
filetype = fromfilename.substring(fromfilename.lastIndexOf("."));
filename = request.getRemoteAddr()+"_"+sdf.format(date)+filetype;
file.saveAs(SavePath+filename);
}
}
msg = "图片上传成功!图片大小为:" + Math.round(file.getSize()/1024) + "K";
if(PhotoUrlID==0){
buf.append("parent.document.myform.photourl_thumb.value='"+request.getContextPath()+SavePath+filename+"';");
buf.append("\n");
buf.append("parent.document.myform.photosize_thumb.value='"+Math.round(file.getSize()/1024)+"';");
buf.append("\n");
}else if(PhotoUrlID==1){
buf.append("parent.document.myform.photourl.value='"+request.getContextPath()+SavePath+filename+"';");
buf.append("\n");
buf.append("parent.document.myform.photosize.value='"+Math.round(file.getSize()/1024)+"';");
buf.append("\n");
}else if(PhotoUrlID==2){
buf.append("parent.document.myform.photourl2.value='"+request.getContextPath()+SavePath+filename+"';");
buf.append("\n");
buf.append("parent.document.myform.photosize2.value='"+Math.round(file.getSize()/1024)+"';");
buf.append("\n");
}else if(PhotoUrlID==3){
buf.append("parent.document.myform.photourl3.value='"+request.getContextPath()+SavePath+filename+"';");
buf.append("\n");
buf.append("parent.document.myform.photosize3.value='"+Math.round(file.getSize()/1024)+"';");
buf.append("\n");
}else if(PhotoUrlID==4){
buf.append("parent.document.myform.photourl4.value='"+request.getContextPath()+SavePath+filename+"';");
buf.append("\n");
buf.append("parent.document.myform.photosize4.value='"+Math.round(file.getSize()/1024)+"';");
buf.append("\n");
}
}
buf.append("alert(\"" + msg + "\");");
buf.append("\n");
buf.append("history.go(-1);");
buf.append("\n");
buf.append("</script>");
pageContext.getOut().println(buf.toString());
}
可我想要的是将图片上传至数据库,而不是文件里.
<%@page contentType="text/html;charset=gb2312" %>
<%@page import="com.jspsmart.upload.*,java.sql.*,java.util.Date;"%>
<%
Attachments attachments = new Attachments();
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
//限制文件上传的大小
int maxFileSize = 10*1024*1024;
su.setMaxFileSize(maxFileSize);
try{
// 文件上传
su.upload();
// 利用Request对象获取参数之值
String wjbh = request.getParameter("wjbh"); //文件编号
String wjbm = request.getParameter("wjbm"); //文件所属表名 // 提取上传文件信息,同时保存文件到数据库
for (int i=0;i<su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i); // 若文件不存在则继续
if (file.isMissing()) continue; //新建一个附件信息
fjgl.FjFjxxObject oFjxx = new fjgl.FjFjxxObject();
oFjxx.setFjbh(new Integer(0));
oFjxx.setFjmc(file.getFileName()); //附件名
oFjxx.setFjlx(file.getContentType()); //附件的文件类型
Integer liBbh = attachments.getMaxBbh(new Integer(wjbh),wjbm,file.getFileName()); //取最新版本号
oFjxx.setBbh(new Integer(liBbh.intValue()+1));
oFjxx.setWjbh(new Integer(wjbh)); //附件所属的文件
oFjxx.setWjbm(wjbm); //附件所属的文件所在的数据库表名
Timestamp ldt_temp = new Timestamp(new Date().getTime()); //取当前时间
oFjxx.setCzrq(ldt_temp);
UserObject userObject = (UserObject)session.getAttribute("userObject");
oFjxx.setCzzg(userObject.getZgbh());
oFjxx.setBz(""); //备注
Integer liFjbh = attachments.saveAttachment(oFjxx); //把文件存入数据库
Connection con = attachments.ds.getConnection();
Statement stm = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
String lsSql = "select * from fj_fjxx where fjbh = " + liFjbh;
ResultSet rs = stm.executeQuery(lsSql);
if (rs.next()){
//FileInputStream str=new FileInputStream(file.getFilePathName());
//rs.updateBinaryStream("fjnr",str,str.available()); file.fileToField(rs,"fjnr");
rs.updateRow();
}
rs.close();
stm.close();
con.close();
}
//跳转页面
response.sendRedirect("/test/testjsp.jsp");
}catch(Exception ex){
response.sendRedirect("/common/ShowError.jsp");
}
%>
可为什么显示出来的就是乱码呢.
下面是我用到的在网页显示图片的一段代码:
//打开数据库
String sql = " SELECT * FROM images WHERE map_name = '2'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
%>
图片:
<%
//获取数据库中的数据
InputStream in = rs.getBinaryStream("map");
//response.reset();
response.setContentType("image/gif");
byte[] b = new byte[1024];
int len;
while((len=in.read(b)) >0)
response.getOutputStream().write(b,0,len);这段代码有错误吗,我用ACCESS库试过,能够将ACCESS库里的图片显示在网页上.
我真是弄不懂了.
<body bgcolor="while">
<H1>文件上载</H1>
<HR>
<form method="POST" action="upload.jsp" ENCTYPE="multipart/form-data">
<input type="FILE" Name="FILE1" size="50"><BR>
<input type=submit value="upload">
</form>
</body>
</html>//=========upload.jsp=======
<%@page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<html>
<body bgcolor="while">
<H1>文件上载</H1>
<HR>
<%
//定义计数变量
int count=0;
//初始化
mySmartUpload.initialize(pageContext);
//设定文件最大上限,这里为100KB
mySmartUpload.setTotalMaxFileSize(1000000);
//调用Bean的实例化对象的方法执行上载操作
mySmartUpload.upload();
try{
//在虚拟目录下的upload目录存储文件
//没有虚拟路径就用物理路径,很熟悉的"/upload"
count=mySmartUpload.save("/upload");
//缺省情况下,上载文件同源文件名称相同
//count=mySmartUpload.save("/upload",mySmartUpload.SAVE_VIRTUAL);
//显示已上载的文件数量
out.println(count+"file(s)uploaded.");
out.pringln("我没有分了........");
}catch (Exception e){
out.println(e.toString());
}
%>
</body>
</html>