免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1585 | 回复: 0
打印 上一主题 下一主题

SmartUpload demo [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-13 22:02 |只看该作者 |倒序浏览

%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
html>
  head>
        title>File Upload/title>
    /head>
    body>
        font size=5 color=#FF0000>
        b>文件上传----使用jspsmart upload组件/b>
        /font>
        br>
        form action="servlet/ServletUpload" method="post"
            enctype="multipart/form-data">
            p>
                文件名称:
                input type="file" name="file1" size="20" maxlength="80">
            /p>
            p>
                文件名称:
                input type="file" name="file2" size="20" maxlength="80">
            /p>
            p>
                文件名称:
                input type="file" name="file3" size="20" maxlength="80">
            /p>
            p>
                上传路径:
                input type="text" name="path" size="30" maxlength="50">
                br>
            /p>
            p>
                附加内容:
                input type="text" name="other" size="30" maxlength="50">
            /p>
            p>
                input type="submit" value="上传">
                input type="reset" value="重置">
            /p>
        /form>
        font size=5 color=#FF0000> b>文件下载----使用jspsmart upload组件/b>
        /font>
        br>
        form action="servlet/ServletDownload" method="post">
            p>
                下载文件的名称:
                input type="text" name="downloadFileName" size="20" maxlength="80">
            /p>
            input type="submit" value="下载">
    /body>
/html>
/*
* ServletUpload.java
*  最关键的几句:mySmartUpload.initialize(config, request, response);
                 mySmartUpload.upload();

                 count = mySmartUpload.save("/upload"); 返回上传文件的个数。
                 一般将文件保存到Web应用程序的根目录/upload,如果不存在此目录,就保存到磁盘的跟目录/upload下。
*
/


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.SmartUpload;
public class ServletUpload extends HttpServlet {
    private ServletConfig config;
    final public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
    }
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("jspSmartUpload : Servlet Sample");
        out.println("");
        // 变量定义
        int count = 0;
        SmartUpload mySmartUpload = new SmartUpload();
        try {
            mySmartUpload.initialize(config, request, response);
            mySmartUpload.upload();
            for (int i = 0; i  mySmartUpload.getFiles().getCount(); i++) {
                com.jspsmart.upload.File myfile = mySmartUpload.getFiles().getFile(i);
                String fileName = myfile.getFileName();
                count = mySmartUpload.save("/upload"); //默认保存到系统根目录upload目录下
                // count = mySmartUpload.save(null);
            }
            out.println(count + " file uploaded.");
        } catch (Exception e) {
            out.println("Unable to upload the file.
");
            out.println("Error : " + e.toString());
        }
        out.println("");
        out.println("");
   
    }
}

ServletDownload.java
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.jspsmart.upload.SmartUpload;
public class ServletDownload extends HttpServlet {
    private ServletConfig config;
    final public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
    }
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String temp_p = request.getParameter("downloadFileName");
        byte[] temp_t = temp_p.getBytes("ISO8859_1");
        String fileName = new String(temp_t, "GBK");
        SmartUpload mySmartUpload = new SmartUpload();
        try {
            mySmartUpload.initialize(config, request, response);
            mySmartUpload.setContentDisposition(null);
            mySmartUpload.downloadFile("/upload/" + fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/49717/showart_1413578.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP