java实现文件上传,文件下载功能 源码

来源:互联网 发布:31岁转行做数据分析师 编辑:程序博客网 时间:2024/04/29 22:42

package test02;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.struts2.ServletActionContext;

public class UploadFile {
 
 public static void main(String[] args) {
  
  
 }
 
 HttpServletRequest request = ServletActionContext.getRequest();
 HttpServletResponse response = ServletActionContext.getResponse();

 /**
     * 下载文件 
     * path:文件详细路径,包括文件名
     */
    public void downloadFile(String path) {
        try {
         // path是指欲下载的文件的路径。
            File file = new File(path);
           
            // 取得文件名。
            String filename = file.getName();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
           

            // 清空response
            response.reset();
           
            // 设置response的Header
            response.setBufferSize(5 * 1024 * 1024);

            filename = new String(filename.getBytes("GBK"), "ISO8859_1");

            response.addHeader("Content-Disposition", "attachment;filename=" + filename);
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");

            int readLength = 0;
            byte[] readUnit = new byte[1024 * 1024];
            while ((readLength = fis.read(readUnit)) != -1) {
                toClient.write(readUnit, 0, readLength);
                toClient.flush();
            }
            fis.close();
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
   
   
    /**
     * 上传文件  (如果是上传同名文件,将先删除原文件,再上传新文件)
     * sPath:文件物理路径,如:c:/apacheTomcat6.0/model
     * uploadFileName:文件名
     * upload:File类型的要上传的文件
     * 返回值:成功
     */
    public String uploadFile(File upload, String uploadFileName, String sPath) {
        try {
            String fileName = uploadFileName;

            // 获取物理路径
            //String sPath = request.getRealPath(filePath);
            sPath = sPath.endsWith(File.separator)?sPath:sPath+File.separator;
           
            String outputFileName = sPath + fileName;

            File outputPathFile = new File(sPath);
            if (!outputPathFile.exists()) {
                outputPathFile.mkdirs();
            }

            //保存文件
            File outputFile = new File(outputFileName);
            InputStream is = new FileInputStream(upload);
            OutputStream os = new FileOutputStream(outputFile);

            if (outputFile.exists())
                outputFile.delete();

            byte buffer[] = new byte[8192];
            int count = 0;
            while ((count = is.read(buffer)) > 0) {
                os.write(buffer, 0, count);
            }

            os.close();
            is.close();

        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
        return "success";
    }
   
}

//Jar包下载链接 http://download.csdn.net/download/da_zhuang/5196573

原创粉丝点击