做一个JSP附件上传MYSQL,用的是smartupload,可以是上传到数据库,也可以保存路径。需求纯JSP源码!!!

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>文件上传</tit... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<p> </p>
<p align="center">上传文件选择</p>
<FORM METHOD="POST" ACTION="jsp/do_upload.jsp"
ENCTYPE="multipart/form-data">
<input type="hidden" name="TEST" value="good">
<table width="75%" border="1" align="center">
<tr>
<td><div align="center">1、
<input type="FILE" name="FILE1" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="上传它!">
</div></td>
</tr>
</table>
</FORM>
</body>
</html>
这是上传页面》》》

<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<%

SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
int count = su.save("/upload");
out.println(count+"个文件上传成功!<br>");
out.println("TEST="+su.getRequest().getParameter("TEST")+"<BR><BR>");

for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);

if (file.isMissing()) continue;

out.println("<TABLE BORDER=1>");
out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
+ file.getFieldName() + "</TD></TR>");
out.println("<TR><TD>文件长度(Size)</TD><TD>" +
file.getSize() + "</TD></TR>");
out.println("<TR><TD>文件名(FileName)</TD><TD>"
+ file.getFileName() + "</TD></TR>");
out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>"
+ file.getFileExt() + "</TD></TR>");
out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
+ file.getFilePathName() + "</TD></TR>");
out.println("</TABLE><BR>");

}
%>
</body>
</html>
这是JSP源码,请问怎么做到数据库里面。求高手帮忙。不要用JAVABEAN。或者其它组件。我不会。谢谢
展开
 我来答
SoSoIT
推荐于2016-08-02 · TA获得超过649个赞
知道小有建树答主
回答量:1780
采纳率:55%
帮助的人:168万
展开全部
你是用什么来做啊;我是Servlet写的

1.连接数据库
package net.qbsp.utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtils {

public static Connection getConn(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String connStr = "jdbc:mysql://localhost:3306/appdb";
String userName = "root";
String password = "admin";
conn = DriverManager.getConnection(connStr,userName,password);
}catch(Exception ex){
System.out.println(ex);
}
return conn;
}
}
2.Get与Set方法
package net.qbsp.domain;

public class Product {

private int id;
private String name;
private String desc;
private String imgPath;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
}

4.servlet类
package net.qbsp.servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.qbsp.dao.ProductDao;
import net.qbsp.domain.Product;
import net.qbsp.utils.CommonTools;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

@SuppressWarnings("serial")
public class ProAddServlet extends HttpServlet{

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(), req, resp);
su.setAllowedFilesList("jpg,gif,png");
su.upload();
Request request = su.getRequest();
String pName = CommonTools.convertGbkToUtf8(request.getParameter("pname"));
String desc = CommonTools.convertGbkToUtf8(request.getParameter("description"));
File file = su.getFiles().getFile(0);
String fileName = System.currentTimeMillis()+"."+file.getFileExt();
System.out.println(UUID.randomUUID().toString());
file.saveAs("/upload/"+fileName, File.SAVEAS_VIRTUAL);
Product p = new Product();
p.setName(pName);
p.setDesc(desc);
p.setImgPath(fileName);
new ProductDao().add(p);
} catch (Exception e) {
e.printStackTrace();
}

}

}

3.Dao操作数据库的java类;
package net.qbsp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.qbsp.domain.Product;
import net.qbsp.utils.DBUtils;
import net.qbsp.utils.PageModel;

public class ProductDao {

public void add(Product p) throws Exception{
Connection conn = DBUtils.getConn();
String sql = "insert into tb_product(pname,pdesc,imgpath) values(?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, p.getName());
pstm.setString(2, p.getDesc());
pstm.setString(3, p.getImgPath());
pstm.executeUpdate();
conn.close();
}
}
5.上传的jsp页面
<%@ page language="java" import="java.util.*" 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>
<base href="<%=basePath%>">

<title>产品添加</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="proadd" method="post" enctype="multipart/form-data">
文件名 :<input name="pname"/>
文件描述:<input name="description"/>
文件图片:<input type="file" name="file">
<input type="submit" value="上传"/>
</form>
</body>
</html>
追问
你好,可以不用Servlet写吗。我是菜鸟不太会用Servlet,希望在同一个JSP页面里面些,这样我会比较理解。如果不嫌弃的话能不能写下代码码参考下。我的邮箱是961921593@qq.com。qq:961921593.求高手帮忙,谢谢。麻烦你们了!!
追答
你把上面我给你的代码分别建java类;把代码复制进去;注意你的数据库连接;我的密码是admin;自己改一下数据库连接的类;就可以运行了啊;如果你要让你的数据显示出来的话;就写一个查询的方法;在写一个jsp页面;可以用EL表达式;
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
专属qi号
2011-10-04
知道答主
回答量:4
采纳率:0%
帮助的人:6706
展开全部
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<p> </p>
<p align="center">上传文件选择</p>
<FORM METHOD="POST" ACTION="jsp/do_upload.jsp"
ENCTYPE="multipart/form-data">
<input type="hidden" name="TEST" value="good">
<table width="75%" border="1" align="center">
<tr>
<td><div align="center">1、
<input type="FILE" name="FILE1" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="上传它!">
</div></td>
</tr>
</table>
</FORM>
</body>
</html>
追问
非常感谢楼上几位帮忙,希望有用纯JSP语言写。楼上没有MYSQL 的连接啊 - -!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式