smartupload上传文件实例代码

 

SmartUpload上传文件实例、关于用jspSmartUpload获得客户端文件的问题代码

smartupload上传文件中应该注意的地方是表单中<form enctype="multipart/form-data" name="form1" action="zhbupload" method="post" >

enctype="multipart/form-data"是必须的,还有用了jspSmartupload以后jsp中的request就失效了,要用Smartupload中的

Request myrequest = null;

mySmartUpload.upload();

myrequest = mySmartUpload.getRequest();

通过myrequest来获取表单中除了上传文件的表单的其他内容!!还有数据库里面file的数据类型一定要是LongBlob!!

package Action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.Calendar;

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

import Other.DataBaseConnection;

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

public class Picupload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        SmartUpload mySmartUpload = new SmartUpload();
        Request myrequest = null;
        String username = "", pwd = "";
        // long file_size_max = 4000000;
        String ext = "";
        String url = "touxiang//"; // 
应保证在根目录中有此目录的存在
        // 
初始化
        mySmartUpload.initialize(this.getServletConfig(), request, response);
        // 
只允许上载此类文件
        try {
            mySmartUpload.setAllowedFilesList("jpg,gif");
            // 
上载文件

            mySmartUpload.upload();
            myrequest = mySmartUpload.getRequest();

        } catch (Exception e) {

            out.print("<script>");
            out
                    .println("alert('
只允许上传.jpg.gif类型图片文件!');window.location='user_register.jsp';");
            out.print("</script>");

        }
        try {
            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(
                    0);
            int count = 0;
            if (myFile.isMissing()) {
                out.print("<script>");
                out.println("alert('
请至少选择一个要上传的文件!');window.location='user_register.jsp';");
                out.print("</script>");

            } else {

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

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

                    saveurl += filename + "." + ext; // 
最终文件的保存路径

                    System.out.println(saveurl);

                    myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);
                    username = myrequest.getParameter("username");
                    pwd = myrequest.getParameter("pwd");

                    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 user(username,pwd,pic,userid) values(?,?,?,?)");
                    pstmt.setString(1, username);
                    pstmt.setString(2, pwd);
                    pstmt.setBinaryStream(3, fis, (int) file.length());
                    pstmt.setInt(4, 1);

                    System.out.println("success inserted user into database");
                    pstmt.execute();
                    pstmt.close();
                    dbc.close();
                }

                out.print("<script>");
                out.print("alert('
上传成功');");
                out.print("window.location='login.jsp'");
                out.print("</script>");
            }
        } catch (Exception e) {

            e.toString();

        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值