求一个jsp实现的上传和下载源代码
小弟在做毕业论文要用到上传照片和下载个人信息。搞不定了,求救

解决方案 »

  1.   

    给你一个简单的例子,你自己想一下吧
    loadfile.html
    <%@page contenttype="text/html;charset=gb2312"%>
    <html>
    <body>
    <p>单击超连接下载zip文档book.zip
    <br>
    <a href="loadfile.jsp">下载book.zip
    </body>
    </html>
    loadfile.jsp
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@page import="java.io.*"%>
    <html>
    <body>
    <%
    OutputStream o=response.getOutputStream();
    byte b[]=new byte[500];
    File fileLoad=new File("E:/aa.mdb");
    response.setHeader("contnt-disposition","attachment; filename="+"aa.rar");
    response.setContentType("application/x-tar");
    long fileLength=fileLoad.length();
    String length1=String.valueOf(fileLength);
    response.setHeader("Content_Length",length1);
    FileInputStream in=new FileInputStream(fileLoad);
    int n;
    while((n=in.read(b))!=-1)
    {
    o.write(b,0,n);
    }
    %>
    </body>
    </html>
    upload.html
    <%@ page contentType="text/html;charset=gb2312"%>
    <html>
    <body>
    <p>选择要上传的文件:<br>
    <form action="upload.jsp" method=post ENCTYPE="multipart/form-data">
    <input type="FILE" name="boy" size="38">
    <br>
    <input type="submit" name="g" value="提交">
    </body>
    </html>
    upload.jsp
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.io.*"%>
    <html>
    <body>
    <% try{//用户的seesion 的id 建立一个临时文件。
    String tempFileName=(String)session.getId();
    //建立临时文件f1.
    File f1=new File("E:/",tempFileName);
    FileOutputStream o=new FileOutputStream(f1);
    //将客户上全部信息存入f1.
    InputStream in=request.getInputStream();
    byte b[]=new byte[1000];
    int n;
    while((n=in.read(b))!=-1)
    {
    o.write(b,0,n);
    }
    o.close();in.close();
    //读取临时文件f1,从中获取上传文件的名字和上传的内容。
    RandomAccessFile random=new RandomAccessFile(f1,"r");
    //读取f1 的第2行,析取上传文件的名字。
    int second=-1;
    String secondLine=null;
    while(second<=2)
    {secondLine=random.readLine();
    second++;
    }
    //获取第2行中'\\'最后出现的位置。
    int position =secondLine.lastIndexOf('\\');
    String fileName=secondLine.substring(position,secondLine.length()-1);
    random.seek(0);//再定位到文件的开头。
    //获取文件的第4 行的回车符号的位置。
    long forthEndPosition=0;
    int forth=1;
    while((n=random.readByte())!=-1&&(forth!=4))
    {
    if(n=='')
    {
    forthEndPosition=random.getFilePointer();
    forth++;
    }
    }
    //根据客户上传文件名字,将该文件存入磁盘。
    File f2=new File("E:/",fileName);
    session.setAttribute("Name",fileName);//供showImage.jsp页面使用
    RandomAccessFile random2=new RandomAccessFile(f2,"rw");
    //确定文件f1中包含客户上传的文件 的内容的最后位置,即倒数第6行。
    random.seek(random.length());
    long endPosition=random.getFilePointer();
    long =endPosition;
    int j=1;
    while((>=0)&&(j<=6))
    {
    --;
    random.seek();
    n=random.readByte();
    if(n=='')
    {
    endPosition=random.getFilePointer();
    j++;
    }
    }
    //将random流指向文件f1的第4 行结束的位置。
    random.seek(forthEndPosition);
    long startPoint=random.getFilePointer();
    //从f1 读出客房上文件 并存入f2(读取从第4 行结束位置和倒数第6行之间的内容)。
    while(startPoint<endPosition-1)
    {
    n=random.readByte();
    random2.write(n);
    startPoint=random.getFilePointer();
    }
    random2.close();
    random.close();
    f1.delete();//删除临时文件。
    }
    catch(IOException e){}
    out.print("文件已上传");
    %>
    </body>
    </html>
    这是一个思路:你自己想一下吧
      

  2.   

    太复杂了,下一个SmartUpload包,用这个做
    <%@ page import="com.jspsmart.upload.*" %>
    SmartUpload su=new SmartUpload();//
    su.initialize(pageContext);
    su.setMaxFileSize(1000000000);
    su.upload(); String[] filePath=new String[su.getFiles().getCount()];

    java.util.Date theDate=new java.util.Date();
    long fileNamePre=theDate.getTime(); for (int i=0;i<su.getFiles().getCount();i++){
    com.jspsmart.upload.File file = su.getFiles().getFile(i);
    String FILENAME=file.getFileName();
    String filedName=file.getFieldName();

    if("saveSite".equals(filedName)){
    if (!file.isMissing()) {
    filePath[i]="moviePic/"+fileNamePre+i+FILENAME.substring(FILENAME.indexOf("."),FILENAME.length());
    file.saveAs(filePath[i],su.SAVE_VIRTUAL);
    saveSite=filePath[i];
    }
    }
    else{
    filePath[i]="";
    }
    }
    String videoName=su.getRequest().getParameter("videoName");
    String tems=su.getRequest().getParameter("temS");
      

  3.   

    呵呵,楼上是要自己处理上传的文件吗?还是用组件吧.apache的jakarta commons项目里有个jspupload.
    自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用.
      

  4.   

    哥哥们~~~求你们了,急用~~
    把全部jsp源文件和要用到的组件发到
      

  5.   

    我的是个简单的组件上传例子哦老大 shenpipi(皮皮) ( ) 信誉:100  2006-03-30 20:27:00  得分: 0  
     
     
       呵呵,楼上是要自己处理上传的文件吗?还是用组件吧.apache的jakarta commons项目里有个jspupload.
    自己处理要考虑的听多,楼上的例子太简单了,只能作为测试例子来用.
      
     
      

  6.   

    谢了,问题解决了一部分,可以上传了
    但是不能request.getParameter()得到非file类型值,
    这个问题解决结分,速度了~~
      

  7.   

    也用SmartUpload中的SmartUpload.getRequstParameter()就可以了啊。
      

  8.   

    http://blog.csdn.net/zdsdiablo/archive/2005/06/16/395713.aspx
    也是用jspsmart的组件