我现在想实现一个上传功能 我不想用jspsmartupload组件
 我想自己写一个上传类
 通过http协议上传
 主要实现的功能是能同时上传多个图片
 并且能把上传form表单的值一并获取过来 ,然后存入到oracle的blob字段中
 [我想通过临时文件上传]
 
 现在遇到了麻烦
  我不知道如何处理了
  
  希望有谁做过这个功能的朋友给说一下思路
  最好有源码的 我先谢过了
  呵呵
   public class getImage extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=GBK";    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }    public void service(HttpServletRequest request, 
                        HttpServletResponse response) throws ServletException, 
                                                             IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        Tool tool = new Tool();
        ServletInputStream inStream = request.getInputStream(); //获取上传文件流
        int streamLenth = request.getContentLength(); //获取上传文件的长度        //将文件存入临时文件中
        FileOutputStream tmpfileStream = null;
        String tmpfileName = null;
        tmpfileName = "c:\\temp\\" + request.getSession().getId();
        tmpfileStream = new FileOutputStream(tmpfileName);
        int bytesRead = 0, totalBytes = 0;
        byte[] cnt;
        while (totalBytes < streamLenth) {
            cnt = new byte[256];
            bytesRead = inStream.read(cnt, 0, 256);
            totalBytes += bytesRead;
            tmpfileStream.write(cnt);
        }
        tmpfileStream.close();
        inStream.close();
        //现已将Http协议上传的内容全部写入了临时文件。以下的代码便可在服务器上对上传的内容进行解释
        //以随机读写方式打开http临时流文件。
        // RandomAccessFile(String name, String mode) - 类 java.io.RandomAccessFile 的构造方法 
        // 创建从中读取和向其中写入(可选)的随机存取文件流,该文件具有指定名称。
        // name - 取决于系统的文件名  mode - 此存取 mode 
        long cntStartPoint = 0, cntEndPoint = 0;
        RandomAccessFile randomFile = null;
        randomFile = new RandomAccessFile(tmpfileName, "rw");
        cntStartPoint = randomFile.getFilePointer();
        String sgmtDeli = randomFile.readLine();
        //out.println(sgmtDeli);
        String line=null;
        boolean eofblock = false;
        while (!eofblock) {
            line = randomFile.readLine();
            //out.println(line+"<br />");
            if(line.equals(sgmtDeli)){
                out.println(line+"<br />");
            }
            if (line.contains(sgmtDeli+"--"))
                eofblock = true;
        }
        
        
        out.close();
    }
    
   // public String cntEndPoint(String line,String sgmtDeli){
   //     String bonuy=
   // }
}
这是我的主要代码
  做到这里我就不知道下一步该怎么做了
  谢谢了

解决方案 »

  1.   

    有jspsmartupload组件干吗不用啊,如果你是要完成项目最好还是用组件,因为组件具有稳定性,简单,没有必要自己去另外写个.
     
    当然如果你是出于学习,到是值得鼓励!请问这个servlet写到这出现了什么问题没有?
      

  2.   

    package yuanyifileup;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.PageContext;public class yuanyifileup
    {
    private ServletRequest request;
    private ServletResponse response;
    private ServletConfig config;
    ServletInputStream DATA;
    int FormSize;
    File f1;
    FileOutputStream os;
    DataInputStream is;
    String filename;
    byte[] b;
    byte t;
    boolean flag=false;
    public yuanyifileup()
    { }
    public void initialize(ServletConfig config,HttpServletRequest request,HttpServletResponse response) throws IOException
    {
    this.request=request;
    this.response=response;
    this.config=config;
    DATA = request.getInputStream();
    FormSize=request.getContentLength();
    }
    public void initialize(PageContext pageContext) throws IOException
    {
    request=pageContext.getRequest();
    response=pageContext.getResponse();
    config=pageContext.getServletConfig();
    DATA = request.getInputStream();
    FormSize=request.getContentLength();
    }
    public boolean setFilename(String s)
    {
    try
    {
    File f1=new File(s);
    os=new FileOutputStream(f1);
    }
    catch(IOException e)
    {return(false);}
    return(true);
    }
    public void getByte()
    {
    int i=0;
    try
    {
    is=new DataInputStream(DATA);
    b=new byte[FormSize];while (true)
    {
    try
    {
    t=is.readByte();
    b[i]=t;
    i++;
    }
    catch(EOFException e)
    { break;}
    }
    is.close();}
    catch(IOException e)
    {}
    }public boolean save()
    {
    int i=0,start1=0,start2=0;
    String temp="";
    if (!flag)
    {
    getByte();
    flag=true;
    }
    try
    {
    temp=new String(b,"ISO8859_1");
    }
    catch(UnsupportedEncodingException e)
    {return(false);}start1=temp.indexOf("image/");
    temp=temp.substring(start1);start1=temp.indexOf(" ");temp=temp.substring(start1+4);
    start2=temp.indexOf("; ");
    if (start2!=-1)
    {
    temp=temp.substring(0,start2);
    }
    try
    {
    byte[] img=temp.getBytes("ISO8859_1");
    for (i=0;i<img.length;i++)
    { os.write(img[i]); }
    os.close();
    }
    catch(IOException e)
    {return(false);}return(true);}
    可以参考一下