谢谢yun15291li(秋飞意)我自己写了几行小程序,不敢独享,张贴如下,请各路牛人修正----------------------------------------------------/*
 * FileUpload.java
 */import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;public class FileUpload {
    /**
     * @param   request
     * @param   size        上传文件的最大大小
     * @throws  ServletException
     * @throws  IOException
     */
    public static ByteArrayInputStream getInputStream(HttpServletRequest request, int size)
    throws ServletException, IOException {
        byte[] b = new byte[size];
        // 文件的实际大小
        int len = 0;
        
        ServletInputStream in = request.getInputStream();
        int i = 0;
        // 跳过封装在请求主体的前面4行头部信息,参考rfc文档有关http协议的部分或通过下面的Monitor.java捕获请求主体
        while ((i++ < 4) && (in.readLine(b, 0, b.length) >= 0));
        int j = 0;
        // 读入封装在请求主体的数据部分
        while (j >= 0) {
            len += j;
            j = in.readLine(b, len, b.length - len);
        }
        // 减去封装在请求主体尾部的多余的48个字节
        len -= 48;
        
        return new ByteArrayInputStream(b, 0, len);
    }
}-------------------------------------------------------

解决方案 »

  1.   

    /*
     * Monitor.java
     */import java.io.*;
    import java.net.*;public class Monitor{
        public static void main(String[] args) {
            try {
                ServerSocket srvSocket = new ServerSocket(5000);
                Socket incoming = srvSocket.accept();
                InputStream in = incoming.getInputStream();
                OutputStream out = incoming.getOutputStream();
                
                byte[] b = new byte[1024];
                // 这个地方有些问题,不知道怎样解决
                for (int len = 0; (len = in.read(b, 0, b.length)) >= 0;)
                    out.write(b, 0, len);
                
                out.close();
                in.close();
                incoming.close();
            } catch (Exception e) {
            }
        }
    }----------------------------------------------------------<!-- fileupload.html --><form action="http://localhost:5000/upload" enctype="multipart/form-data" method="post">
      <input type="file" name="file" size="30"><br><br>
      <input type="submit">
    </form>
      

  2.   

    servlet代码片段:protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        try {
            Context context = new InitialContext();
            DataSource dataSource = (DataSource)context.lookup("jdbc/test");  
            Connection con = dataSource.getConnection();
            Statement stmt = con.createStatement();
            
            InputStream in = FileUpload.getInputStream(request, 512000);
                
            PreparedStatement prepStmt = con.prepareStatement("INSERT INTO UPLOAD(DATA) VALUES(?)");
            prepStmt.setBinaryStream(1, in, in.available());
            prepStmt.execute();
                
            in.close();
            prepStmt.close();
            stmt.close();
            con.close();
        } catch (Exception e) {
        }
    }
      

  3.   

    从数据库读文件:    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            String imageid = request.getParameter("fileid");
            
            byte[] b = new byte[512000];
            int len = 0;
            
            try {
                Context context = new InitialContext();
                DataSource dataSource = (DataSource)context.lookup("jdbc/test");  
                Connection con = dataSource.getConnection();
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT DATA FROM UPLOAD WHERE ID = " + fileid);
                rs.next();
                
                Blob File = rs.getBlob(1);
                len = (int)File.length();
                
                BufferedInputStream bis = new BufferedInputStream(File.getBinaryStream());
                bis.read(b, 0, len);
                bis.close();
                
                OutputStream out = response.getOutputStream();
                out.write(b, 0, len);
                out.close();
                
                rs.close();
                stmt.close();
                con.close();
            } catch (Exception e) {
            }
        }
      

  4.   

    输出图片怎么没有setContentType