用jspsmartupload,google搜一下

解决方案 »

  1.   

    我的意思是说白了就是如何用JSP来实现文件的下载和上传,上传的话我也知道一点儿,关键是下载呀?
      

  2.   


    解决方案:1。  直接应运 jspSmartUpload  中提供的方法。
     
         方法: 
            // Initialization 
    mySmartUpload.initialize(pageContext);

    // Download file  如果你是 linux 服务器那么你可以这样做:
    mySmartUpload.downloadFile("/服务器软件配置的路径(可以不填写)/相对路径/sample.zip");

    // With a physical path 如果是 window 的服务器
    // mySmartUpload.downloadFile("c:\\temp\\sample.zip")

    // With options
    // mySmartUpload.downloadFile("/upload/sample.zip","application/x-zip-compressed","downloaded.zip")
    2。 如果是 linux 服务器 那么直接的到文件的路径,和文件名。作为一个链接点击可以直接下载。
        值得注意的是 : 
                如果文件为 .txt 文件那么将在 浏览器里面直接显示。
      如果是 图片如: gif 那么一样在浏览器里面直接打开。
        如果为其他文件则直接下载。     解决 办法: 下载时将他们作为 .zip 文件直接下载。    public void downloadFile(java.lang.String sourceFilePathName,
                             java.lang.String contentType,
                             java.lang.String destFileName)
      

  3.   

    关于文件上传和下载的完全解惑:
    先说上穿
    upload.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
    <html>
    <head>
      <title>文件上载</title>
    </head>
    <body>
    <center>
    <form name="uploadfile" method="post" onsubmit="" enctype="multipart/form-data"  action="UpLoadMedia.jsp">
        <table width="100%" border="0">
          <tr>
            <td width="29%" align="right" valign="middle"><span class="style2">请选择需要上传的文件:</span></td>
            <td width="30%"><input name="filePath" type="file" class=adminbutton id="filePath" size="30"></td>
            <td width="41%"><input name="upload" type="submit" class=adminbutton  value="上载"></td>
          </tr>
        </table>
    </form>
    </center>
    </body>
    </html>----------------------------
    UpLoadMedia.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %>
    <jsp:useBean id="upload1" scope="page" class="Memory.UpLoad" >
    <jsp:setProperty name="upload1" property="uploadDirectory" value="/home/bigs.com.cn/web/www/temp/" />
    </jsp:useBean>
    <%
    String filename = upload1.uploadFile(request);
    %><html<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <body>
    <center>
      <p><font  color=red>文件<%=toChinese(filename)%>成功上载!</font>
    </p>
      <p><a href="msend.jsp">返回</a></p>
    </center>  
    </body>
    </html>----------------------------------
    UpLoad Bean的代码 UpLoad.javapackage Memory;import  java.io.*;
    import  javax.servlet.http.HttpServletRequest;
    import  javax.servlet.ServletInputStream;
    import  javax.servlet.ServletException;public  class  UpLoad {
      private  static  String  newline  =  "\n";
      private  String  uploadDirectory  =  ".";
      private  String  ContentType  =  "";
      private  String  CharacterEncoding  =  "";
      String  filename  =  null;
      
      public UpLoad() {
      }  private String getFileName(String s) {
        int  i  =  s.lastIndexOf("\\");
        if(i  <  0  ||  i  >=  s.length()  -  1) {
          i  =  s.lastIndexOf("/");
          if(i  <  0  ||  i  >=  s.length()  -  1)
            return  s;
        }
        return  s.substring(i  +  1);
      }  public  void  setUploadDirectory(String  s) {
        uploadDirectory  =  s;
      }  public  void  setContentType(String  s) {
        ContentType  =  s;
        int  j;
        if((j  =  ContentType.indexOf("boundary="))  !=  -1) {
          ContentType  =  ContentType.substring(j  +  9);
          ContentType  =  "--"  +  ContentType;
        }
      }  public  void  setCharacterEncoding(String  s) {
        CharacterEncoding  =  s;
      }  public  String  uploadFile(  HttpServletRequest  req)   throws  ServletException,  IOException {
        setCharacterEncoding(req.getCharacterEncoding());
        setContentType(req.getContentType());
        uploadFile(req.getInputStream());
        return filename;
      }  public  void  uploadFile(  ServletInputStream  servletinputstream)  throws
        ServletException,  IOException {    String  s5  =  null;
        byte  Linebyte[]  =  new  byte[4096];
        byte  outLinebyte[]  =  new  byte[4096];
        int  ai[]  =  new  int[1];
        int  ai1[]  =  new  int[1];    String  line;
        while((line  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null) {
          int  i  =  line.indexOf("filename=");
          if(i  >=  0) {
            line  =  line.substring(i  +  10);
            if((i  =  line.indexOf("\""))  >  0)
            line  =  line.substring(0,  i);
            break;
          }
        }    filename  =  line;    if(filename  !=  null  &&  !filename.equals("\"")) {
          filename  =  getFileName(filename);      String  sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);
          if(sContentType.indexOf("Content-Type")  >=  0)
          readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);      File  file  =  new  File(uploadDirectory,  filename);      FileOutputStream  fileoutputstream  =  new  FileOutputStream(file);      while((sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null) {
            if(sContentType.indexOf(ContentType)  ==  0  &&  Linebyte[0]  ==  45)
    break; if(s5  !=  null) {
      fileoutputstream.write(outLinebyte,  0,  ai1[0]);
      fileoutputstream.flush();
    }
    s5  =  readLine(outLinebyte,  ai1,  servletinputstream,  CharacterEncoding);
    if(s5  ==  null  ||  s5.indexOf(ContentType)  ==  0  &&  outLinebyte[0]  ==  45)
      break;
    fileoutputstream.write(Linebyte,  0,  ai[0]);
    fileoutputstream.flush();
          }      byte  byte0;
          if(newline.length()  ==  1)
            byte0  =  2;
          else
            byte0  =  1;
          if(s5  !=  null  &&  outLinebyte[0]  !=  45  &&  ai1[0]  >  newline.length()  *  byte0)
            fileoutputstream.write(outLinebyte,  0,  ai1[0]  -  newline.length()  *  byte0);
          if(sContentType  !=  null  &&  Linebyte[0]  !=  45  &&  ai[0]  >  newline.length()  *  byte0)
            fileoutputstream.write(Linebyte,  0,  ai[0]  -  newline.length()  *  byte0);
          fileoutputstream.close();
        }
      }  private  String  readLine(byte  Linebyte[],  int  ai[], ServletInputStream  servletinputstream,
        String  CharacterEncoding) {    try{
          ai[0]  =  servletinputstream.readLine(Linebyte,  0,  Linebyte.length);
          if(ai[0]  ==  -1)
            return  null;
        } catch(IOException  _ex) {
          return  null;
        }
        
        try {
          if(CharacterEncoding  ==  null) {
            return  new  String(Linebyte,  0,  ai[0]);
          } else {
            return  new  String(Linebyte,  0,  ai[0],  CharacterEncoding);
          }
        } catch(Exception  _ex) {
          return  null;
        }
      }
    }
    --------------------------------------
    上传功能实现,请给50分
      

  4.   

    下面说下载
    下载其实更加简单请看下面的代码:
    download.jsp<%@ page contentType="text/html; charset=gbk"%>
    <%@ page language="java" import="java.util.*"%>
    <%@ page import="java.lang.*"%>
    <%@ page import="javax.mail.*"%>
    <%@ page import="javax.mail.internet.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.text.SimpleDateFormat"%><%
    try
    {
    //BufferedInputStream is = new BufferedInputStream(part.getInputStream());FileInputStream is = new FileInputStream("文件的完整路径"); response.setContentType("application/octet-stream");
    response.setHeader ("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 
    response.setHeader("Accept-ranges", "bytes"); ServletOutputStream output = response.getOutputStream(); byte[] buf=null; int l=0; while ((buf[i] = is.read()) != -1)   
    {
    output.write(buf[i]);
    i++;
    }
    output.write(buf); is.close();
    output.close();}catch(javax.mail.MessagingException  e){
        System.out.println(e.getMessage());
    }
    %>原理就是把服务器文件打开成一个InputStream,然后通过
    response.setContentType("application/octet-stream");
    response.setHeader ("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 
    response.setHeader("Accept-ranges", "bytes");
    这样的操作给向浏览器输出的流+一个header 让浏览知道我是一个比特流,名字是什么,实现了下载。
      

  5.   

    我的代码是从我自己的网站下来的,因为我的网站不涉及直接文件操作,会有一些地方有些小错误,望包涵。此外,我没有鄙视smartupload的意思,不过这个东西的存在一定程度上阻碍了很多人在文件操作方面对java的开发
      

  6.   

    <!--
    文件名:download.html
    作  者:纵横软件制作中心雨亦奇([email protected])
    -->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>下载</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
    <a href="jsp/do_download.jsp">点击下载</a>
    </body>
    </html>
     
    2、下载处理页面do_download.jsp do_download.jsp展示了如何利用jspSmartUpload组件来下载文件,从下面的源码中就可以看到,下载何其简单。 源码如下: <%@ page contentType="text/html;charset=gb2312" 
    import="com.jspsmart.upload.*" %><%
    // 新建一个SmartUpload对象
    SmartUpload su = new SmartUpload();
    // 初始化
    su.initialize(pageContext);
    // 设定contentDisposition为null以禁止浏览器自动打开文件,
    //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
    //doc时,浏览器将自动用word打开它。扩展名为pdf时,
    //浏览器将用acrobat打开。
    su.setContentDisposition(null);
    // 下载文件
    su.downloadFile("/upload/如何赚取我的第一桶金.doc");
    %>
     
      

  7.   

    不是吧,直接用连接指向一个文件,不就下载了,只要不是.jsp.html等就行了
      

  8.   

    小弟用(天平座的光辉)的方法实现了,不过发现一个问题就是在上传时,当上传文件过大达到几十M的时候就报错,说什么Outof memory,而且内存使用量狂长。
    TO(蓝鲸):非常感激你,不过我却没有办法根据你的代码来实现,当然小弟是新手,所以后来就选择了smartupload,还请指正。
      

  9.   

    可以反编译一下 smartupload的代码看看的啊 不怎么长的
      

  10.   

    jspsmartupload这个包中有个例子
      

  11.   

    the source code of the SmartUpload is good enough.