码迷,mamicode.com
首页 > 编程语言 > 详细

Java文件上传下载

时间:2016-04-22 20:07:09      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:

Java文件上传下载

基本原理

文件上传原理
通过为表单元素设置Method=”post” enctype=”multipart/form-data”属性,让表单提交的数据以二进制编码的方式提交,在接收此请求的Servlet中用二进制流来获取内容,就可以取得上传的文件的内容,从而实现文件的上传。
技术分享
文件下载原理
技术分享

JSP+Servlet实现文件上传下载

上传

技术分享

    <form action="<%=path %>/uploadServlet.do" method="post" enctype="multipart/form-data">
        请选择图片:<input id="myfile" name="myfile" type="file" >
        <input type="submit" value="提交" />
    </form>

技术分享

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("已接收到请求");
        //从request当中获取流信息
        InputStream fileSource = req.getInputStream();
        String tempFileName = "E:/tempFile";
        //tempFile指向临时文件
        File tempFile = new File(tempFileName);
        //outputSteam文件输出流指向这个临时文件
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        byte b[] = new byte[1024];
        int n;
        while((n = fileSource.read(b)) != -1){
            outputStream.write(b, 0, n);
        }
        //关闭输出流、输入流
        outputStream.close();
        fileSource.close();

        //获取上传的文件的名称
        RandomAccessFile randomFile = new RandomAccessFile(tempFile, "r");
        randomFile.readLine();
        String str = randomFile.readLine();
        int beginIndex = str.lastIndexOf("\\")+1;
        int endIndex = str.lastIndexOf("\"");
        String filename = str.substring(beginIndex, endIndex);

        //重新定位文件指针到文件头
        randomFile.seek(0);
        long startPostion = 0;
        int i = 1;
        //获取文件内容的开始位置
        while((n = randomFile.readByte()) != -1 && i <= 4){
            if(n == ‘\n‘){
                startPostion = randomFile.getFilePointer();
                i++;
            }
        }
        startPostion = startPostion - 1;
        //获取文件内容的结束位置
        randomFile.seek(randomFile.length());
        long endPosition = randomFile.getFilePointer();
        int j = 1;
        while(endPosition >= 0 && j<=2){
            endPosition--;
            randomFile.seek(endPosition);
            if(randomFile.readByte() == ‘\n‘){
                j++;
            }
        }
        endPosition = endPosition - 1;

        //设置保存文件上传文件的路径
        String realPath = getServletContext().getRealPath("/")+"images";
        File fileupload = new File(realPath);
        if(!fileupload.exists()){
            fileupload.mkdir();
        }
        File saveFile = new File(realPath, filename);
        RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile, "rw");
        //从临时文件中读取文件内容(根据起止位置获取)
        randomAccessFile.seek(startPostion);
        while(startPostion > endPosition){
            randomAccessFile.write(randomFile.readByte());
            startPostion = randomFile.getFilePointer();
        }
        //关闭输入输出流、删除临时文件
        randomAccessFile.close();
        randomFile.close();
        tempFile.delete();

        req.setAttribute("result", "上传成功!");
        RequestDispatcher dispatcher = req.getRequestDispatcher("jsp/01.jsp");
        dispatcher.forward(req, resp);
    }

下载

下载实现思路
技术分享

<a href="<%=path %>/downloadServlet.do?filename=00.jpg">text.txt</a>
package com.imooc.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class DownloadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取文件下载路径
        String path = getServletContext().getRealPath("/")+"images/";
        String filename = req.getParameter("filename");
        File file = new File(path+filename);
        if (file.exists()) {
            //设置相应类型
            resp.setContentType("application/x-msdownlaod");
            //设置头信息
            resp.setHeader("Content-Disposition", "attachment;filename=\""+filename+"\"");
            InputStream inputStream = new FileInputStream(file);
            ServletOutputStream outputStream = resp.getOutputStream();
            byte b[] = new byte[1024];
            int n;
            while((n = inputStream.read(b)) != -1){
                outputStream.write(b, 0, n);
            }
            //关闭流、释放资源
            outputStream.close();
            inputStream.close();

        }else{
            req.setAttribute("errorResult", "文件不存在,下载失败!");

        }
    }

}

SmartUpload

上传
如下上传文件:

    <form action="smartupload_demo01.jsp" method="post" enctype="multipart/form-data">
        选择文件1:<input type="file" name="myfile1"><br/>
        选择文件2:<input type="file" name="myfile2"><br/>
        选择文件3:<input type="file" name="myfile3"><br/>
        <input type="submit" value="上传">
    </form>

处理过程如下:

        //设置上传文件保存路径
        String filePath = getServletContext().getRealPath("/")+"images";
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdir();
        }

        SmartUpload su = new SmartUpload();
        //初始化对象
        su.initialize(getServletConfig(), request, response);
        //设置上传文件大小
        su.setMaxFileSize(1024*1024*10);
        //设置所有文件大小
        su.setTotalMaxFileSize(1024*1024*100);
        //设置允许用户上传文件类型
        su.setAllowedFilesList("txt,jpg,png,gif");
        String result    = "上传成功";
        //设置禁止上传文件类型
        try{
            su.setDeniedFilesList("rar,jsp,js");
            //上传文件
            su.upload();
            int count = su.save(filePath);


        }catch(Exception e){
            e.printStackTrace();
            result = "上传失败";
        }

异常处理

            if(e.getMessage().indexOf("1015") != -1){
                result = "上传失败:上传文件类型不正确!";
            }else if(e.getMessage().indexOf("1010") != -1){
                result = "上传失败:上传文件类型不正确!";
            }else if(e.getMessage().indexOf("1105") != -1){
                result = "上传失败:上传文件大小大于允许的最大值!";
            }else if(e.getMessage().indexOf("1110") != -1){
                result = "上传失败:上传文件总大小大于允许的最大值!";
            }

通过SmartUpload获取上传文件的其他属性

        for(int i = 0; i < su.getFiles().getCount(); i++){
            org.lxh.smart.File tempFile = su.getFiles().getFile(i);
            System.out.println("--------");
            System.out.println("表单当中name的值:"+tempFile.getFieldName());
            System.out.println("上传文件名:"+tempFile.getFileName());
            System.out.println("上传文件的大小:"+tempFile.getSize());
            System.out.println("上传文件的拓展名:"+tempFile.getFileExt());
            System.out.println("上传文件的全名:"+tempFile.getFilePathName());
            System.out.println("--------");
        }

输出结果如下:

表单当中name的值:myfile3
上传文件名:2.png
上传文件的大小:316681
上传文件的拓展名:png
上传文件的全名:2.png

下载

下载:<a href="smartDownloadServlet.do?filename=img2-lg.jpg">img2-lg.jpg</a>

SmartDownloadServlet如下:

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String filename = req.getParameter("filename");
        SmartUpload su = new SmartUpload();
        su.initialize(getServletConfig(), req, resp);
        su.setContentDisposition(null);
        try {
            su.downloadFile("/images/"+filename);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

    }

使用SmartUpload实现文件批量下载
如何实现文件的批量下载?

    <form action="smartDownloadServlet.do">
        <input type="checkbox"  name="filename" value="img2-lg.jpg">Image2
        <input type="checkbox"  name="filename" value="img3-lg.jpg">Image3
        <input type="checkbox"  name="filename" value="img4-lg.jpg">Image4
        <input type="submit" value="下载">
    </form>

servlet处理

        resp.setContentType("application/x-msdownload");
        resp.setHeader("Content-Disposition", "attachment;filename=test.zip");

        String filepath = getServletContext().getRealPath("/")+"images/";
        String[] filenames = req.getParameterValues("filename");
        String str = "";
        String rt = "\r\n";
        ZipOutputStream zipOutputStream  = new ZipOutputStream(resp.getOutputStream());
        for(String filename : filenames){
            str += filename+rt;
            File file = new File(filepath+filename);
            zipOutputStream.putNextEntry(new ZipEntry(filename));
            FileInputStream fis = new FileInputStream(file);
            byte b[] = new byte[1024];
            int n = 0;
            while((n = fis.read(b)) != -1){
                zipOutputStream.write(b, 0, n);
            }
            zipOutputStream.flush();
            fis.close();
        }
        zipOutputStream.setComment("download success:"+rt+str);
        zipOutputStream.flush();
        zipOutputStream.close();

Java文件上传下载

标签:

原文地址:http://blog.csdn.net/winfredzen/article/details/51203383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!