menu
more_vert

jsp实现文件上传下载

作者:dayu

文件上传:

upload.jsp

    <form action="uploadServlet" method="post" enctype="multipart/form-data">
        作者:<input type="text" name="author"/><br/>
        文件1:<input type="file" name="file"/><br/>
        文件2:<input type="file" name="file"/><br/>
        <input type="submit" value="上传"/>
    </form>

UploadServlet:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;

public class UploadServlet extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String author1 = request.getParameter("author");
        System.out.println("author1="+author1);
        
        //创建smartUpload组件对象
        SmartUpload smartUpload = new SmartUpload();
        //初始化当前对象
        smartUpload.initialize(this.getServletConfig(), request, response);
        
        //控制总文件的上传大小(单位B)(10MB)
        smartUpload.setTotalMaxFileSize(1024*1024*10);
        //控制单个文件的上传大小(单位B)(1MB)
        smartUpload.setMaxFileSize(1024*1024);
        
        try {
            //上传文件
            smartUpload.upload();
            //获取smartUpload组件里的request对象
            Request req = smartUpload.getRequest();
            String author = req.getParameter("author");
            System.out.println("author="+author);
            
            //获取上传文件
            Files files = smartUpload.getFiles();
            //获取单个文件
            for(int i=0;i<files.getCount();i++){
                File file = files.getFile(i);
                file.saveAs("d:\\upload\\"+file.getFileName());
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

文件下载:

download.jsp

<img src="img/wa.png"><a href="downLoadServlet?fileName=wawa.jpg">下载</a>

DownloadServlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class DownLoadServlet extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //获取下载的文件名
        String fileName = request.getParameter("fileName");
        
        //创建smartUpload组件对象
        SmartUpload smartUpload = new SmartUpload();
        //初始化当前对象
        smartUpload.initialize(this.getServletConfig(), request, response);
        //禁止浏览器自动打开下载的文件
        smartUpload.setContentDisposition(null);
        
        try {
            smartUpload.downloadFile("download/"+fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

jsp实现文件上传下载

原文地址:http://www.cnblogs.com/Vito-Yan/p/7224266.html



Processed Time:0.20642495155334

_REQUEST:
Array
(
    [0] => read
    [1] => 95012
)
_SESSION:
Array
(
    [fid] => 4
)