要实现上传文件(图片)保存到数据库mysql
请问有什么第三方组件可以简单实现
我查到jspSmartUpload能将文件上传到数据库中,也能将数据库中的数据下载下来。这种功能针对的是MYSQL数据库
请大家提供用jspSmartUpload上传文件(图片)到mysql的例子等学会了这个再来请教其他通用的方法
谢谢

解决方案 »

  1.   

    用JspSmartupload上传文件一般都是将上传的文件转存到服务器上,然后将路径保存到数据库中,
    将文件保存到数据库中下载的时候效率很低。
    LZ参考一下下面的代码:package smartupload;import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.PreparedStatement;
    import java.text.SimpleDateFormat;
    //import java.util.Calendar;
    import java.util.Date;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.Request;
    import com.jspsmart.upload.SmartUpload;
    //import com.jspsmart.upload.SmartUploadException;public class UploadFile extends HttpServlet 
    {
    private static final long serialVersionUID = 1L;
    @SuppressWarnings({ "static-access", "deprecation" })
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
    response.setContentType("text/html;charset=gb2312");
    PrintWriter out = response.getWriter();

    SmartUpload mySmartUpload = new SmartUpload();
    @SuppressWarnings("unused")
    Request myrequest = null;
    // String title =null;

    mySmartUpload.initialize(this.getServletConfig(),request,response);// 初始化

    try 
    {
    mySmartUpload.setAllowedFilesList("doc,txt,pdf,rar,gif,jpg,ppt,xml");// 只允许上载此类文件
    mySmartUpload.upload();// 上载文件
    myrequest = mySmartUpload.getRequest();

    catch (Exception e) 
    {
    out.print("<script>");
    out.println("alert('只允许上传doc,txt,pdf,rar,gif,jpg,ppt类型的文件!');window.location='index.jsp';");
    out.print("</script>");
    }
    try 
    {
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    @SuppressWarnings("unused")
    int count = 0;
    if (myFile.isMissing())
    {
    out.print("<script>");
    out.println("alert('请至少选择一个要上传的文件!');window.location='index.jsp';");
    out.print("</script>");

    else 
    {
    out.println("正在上传文件,请等待。。");
    for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) 
    {
    myFile = mySmartUpload.getFiles().getFile(i);
    if (myFile.isMissing())
    continue;
    String ext = myFile.getFileExt(); // 取得后缀名
    @SuppressWarnings("unused")
    int file_size = myFile.getSize(); // 取得文件的大小
    String saveurl=null;

    Date dt = null; 
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    dt = new Date(System.currentTimeMillis()); 
    //             String newfilename = fmt.format(dt)+ filename.substring(filename.lastIndexOf("."));

    // Calendar calendar = Calendar.getInstance();// 更改文件名,取得当前上传时间的毫秒数值
    String filename =fmt.format(dt);
    saveurl = request.getRealPath("\\")+"file\\";

    saveurl +=filename + "." + ext;// 最终文件的保存路径
    String name= filename + "." + ext;  System.out.println(saveurl); myFile.saveAs(saveurl, mySmartUpload.SAVE_VIRTUAL);
    // title = myrequest.getParameter("title"); PreparedStatement pstmt = null;
    DataBaseConnection dbc = null;
    dbc = new DataBaseConnection();// FileInputStream fis;
    // File file;
    //
    // file = new File(saveurl);
    // fis = new FileInputStream(file);


    pstmt = dbc.getConnection().prepareStatement("insert into file(name) values(?)");
    pstmt.setString(1,name);
    // pstmt.setBinaryStream(2, fis, (int) file.length());
    System.out.println("success inserted file into database");
    pstmt.execute();
    pstmt.close();
    dbc.close();
    } out.print("<script>");
    out.print("alert('上传成功');");
    out.print("window.location='list.jsp'");
    out.print("</script>");
    }
    }
    catch (Exception e) 
    {
    e.toString(); }
    out.flush();
    out.close();
    }
    }
      

  2.   

    经过自己查找、调试
    已经实现一个简单用jspSmartUpload上传文件(图片)到mysql了
    现在请高手指点高进
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.jspsmart.upload.*;
    import java.sql.*;
    public class imageUpload extends HttpServlet {
    private ServletConfig config;
    //初始化Servlet
    final public void init(ServletConfig config) throws ServletException {
    this.config = config;
    }
    //处理GET请求
    public void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("The method of the HTML form must be POST.");
    }
    //响应POST请求
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=GBK");
    PrintWriter out = response.getWriter();

    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException ce)
    {
    out.println(ce.getMessage());
    }
    try {
    //建立数据库连接
    String url="jdbc:mysql://localhost:3306/test";
    String user="root";
    String pass="";
    Connection conn=DriverManager.getConnection(url,user,pass); SmartUpload mySmartUpload = new SmartUpload();
    //初始化
    mySmartUpload.initialize(config,request,response);
    mySmartUpload.upload();
    //建立File对象
    com.jspsmart.upload.File   file   =   mySmartUpload.getFiles().getFile(0);
    String name=file.getFileName();
    int   size   =   file.getSize();     
    byte[]   image   =   new   byte[size];
    //转化成二进制   
    for(int   i   =   0;   i   <   size;   i++)   
    {   
    image[i]   =   file.getBinaryData(i);   
    }   
    //存储到数据库
    String sql="insert into image(name,file) value(?,?)";
    PreparedStatement pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,name);
    pstmt.setBytes(2, image);
    pstmt.execute();
    pstmt.close();
    conn.close();
    out.println(name+"存储成功!");


    } catch (Exception e){
    out.println("Unable to upload the file.<br>");
    out.println("Error : " + e.toString());
    }
        }
    }
      

  3.   

    现在还有个问题
    已经将文件存成二进制格式在数据库
    如果要从数据库下载文件显示
    还需要用jspSmartUpload吗,方便吗
    具体怎么显示