用jspsmartupload进行上传下载(转自http://www.mecee.com/article/s926)

这是进行上传

import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;

public class UpLoad extends HttpServlet {

    private ServletConfig config;
    /**
     * Init the servlet
     */
    final public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }

    final public ServletConfig getServletConfig() {
        return config;
    }

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

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("============UpLoad begin ddd=============");
        String id = null;
        SmartUpload mySmartUpload = new SmartUpload();
        PrintWriter out = response.getWriter();
        java.io.File file=null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: " + e.getMessage());
        }

        try {
            //Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL", "test", "test");

            conn.setAutoCommit(false); //设置手动提交,以提高性能。
            Statement stmt = conn.createStatement();
            // Initialization
            mySmartUpload.initialize(config, request, response);

            mySmartUpload.setMaxFileSize(500 * 1024);

            // Upload
            mySmartUpload.upload();

            //取得text框中的数据
            id = mySmartUpload.getRequest().getParameter("id");

            //取得文件和文件名
            com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
            String fileName = myFile.getFileName();

            if (!myFile.isMissing()) {
                //save data
                myFile.saveAs(fileName, mySmartUpload.SAVE_PHYSICAL);
                System.out.println("============fileName=" + fileName + "====");
                file = new java.io.File(fileName);

                java.io.InputStream inStream = new java.io.FileInputStream(file);

                int fileSize = myFile.getSize();
                byte[] bytes = new byte[fileSize];
                System.out.println("============fileSize=" + fileSize + "====");

                //将数据插入到表中
                String strSql = "insert into test(id,name,content) values(’" + id + "’,’" + fileName + "’,empty_blob())";
                stmt.execute(strSql);

                ResultSet rs = stmt.executeQuery("select content from zyw_test where id=’" + id + "’ for update ");
                if (rs.next()) {
                    oracle.sql.BLOB blob = ( (oracle.jdbc.OracleResultSet) rs).getBLOB("content");
                    OutputStream outStream = blob.getBinaryOutputStream();
                    inStream.read(bytes);
                    outStream.write(bytes);
                    outStream.flush();
                    stmt.execute("commit");
                    outStream.close();
                }
                inStream.close();
                stmt.close();

                if(file!=null)//删除文件
                    file.delete();

                out.println("upload sucess");
            } else {
                out.println("no file");
            }
        } catch (Exception ex) {
            if(file!=null)
                file.delete();
            out.println("upload fail");
            ex.printStackTrace();
        }

        System.out.println("============UpLoad end=============");
    }
}

这是下载,进行显示图片


import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownLoad extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        System.out.println("======DownLoad begin=====");
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","test","test");
            Statement stmt=conn.createStatement();
            String id=request.getParameter("id");
            String sql="SELECT id, name, content FROM test where id=’"+id+"’";
            ResultSet rs=stmt.executeQuery(sql);
            if(rs.next()){
                Blob blob = rs.getBlob("content");
                byte[] ab = blob.getBytes(1, (int)blob.length());
                response.setContentType("image/jpeg");
                ServletOutputStream op = response.getOutputStream();
                op.write(ab);
                op.flush();
                op.close();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println("======DownLoad end=====");
    }
}

你可能感兴趣的:(用jspsmartupload进行上传下载(转自http://www.mecee.com/article/s926))