java中下载文件获取文件路径和截取字符串的一点记录

本文记录了在Java中处理文件下载时如何获取文件路径,以及如何处理字符串截取的问题。文章提到了IDEA与Eclipse在保存静态资源路径的区别,并展示了如何设置IDEA使输出目录与Eclipse保持一致。在实际操作中,通过数据库获取到的文件路径通常以'/'开头,需要使用File类进行修正。此外,还简述了在JSP页面中进行文件上传、下载和删除的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始之前,先说点题外话

由于IDEA上传的文件静态资源,保存的路径和eclipse有些区别,之前用eclipse是保存在WEB-INF下,

当然我们也可以修改output directory,和eclipse一样放在WEB-INF目录下,操作如下,编辑tomcat

1

23

@RequestMapping(value = "download")
public void download(Catalog catalog,HttpServletRequest request, HttpServletResponse response){
    //获取服务器绝对路径, 一般在项目下target路径 
    String rootPath = request.getSession().getServletContext().getRealPath("/");
    //从数据库获取文件名(此名包括路径,且为URL编码需解码)
    String info = catalog.getInfo();
    String fileName = "";
    try {
        //将URL的文件名转码为UTF-8(调用java.net.URLDecoder的方法解码)
        fileName = URLDecoder.decode(info, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    //调用FileUtils.downFile下载(FileUtils已经写好的文件工具类)
    FileUtils.downFile(new File(rootPath+fileName),request,response);
}

其实这样已经可以完成下载了,下面说下关于上传和下载中获取文件路径,还是以上面的下载为例分解说明   

@RequestMapping(value = "download")
public void download(Catalog catalog,HttpServletRequest request, HttpServletResponse response){
        //这里讲下获取上传文件的路径

        //1.获取服务器的绝对路径,一般在项目下target路径  String rootPath = request.getSession().getServletContext().getRealPath("/");
        //2.对于配置了basedir的可以直接调用方法   String rootPath = Global.getUserfilesBaseDir();
        //3.对于上传指定了路径    String rootPath = "D:\\workspace\\file\\";  

        //获取数据库中文件名(此包括虚拟路径,且为URL编码)
        String info = catalog.getInfo();
        //java.io.File将数据库中"/"转义"\"
        File file = new File(info);
        String file1 = file.getPath();
        String file2 = info.split("\\\\")[0];
        //这里split("\\\\")做下说明,正则中以"\\"表示"\",java中也是以"\\"表示"\",所以即"\\\\"

        String fileName1 = "";
        String fileName2 = "";

        //File中file.getParent()方法获取文件的上级目录
        String path1 = file.getParent();
        //用String.substring()方法截取文件的上级目录,另外也可以用split()方法截取,用split截取的是数组
        String path2 =info.substring(info.indexOf("/"),info.lastIndexOf("/"));

        //在示例单独截取只文件名和扩展名
        String flName = info.substring(info.lastIndexOf("/")+1);
        String flEtx = info.substring(info.lastIndexOf("."));

        //下面一样需要解码
        try {
            //将URL编码的文件名解码为UTF-8格式(调用java.net.URLDecoder的方法解码)
            fileName1 = URLDecoder.decode(file1, "UTF-8");
            fileName2 = URLDecoder.decode(file2, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        }

        //调用FileUtils.downFile下载
        FileUtils.downFile(new File(rootPath+fileName1),request,response);
  }

以debug执行看看结果,如下

从结果看,从数据库中得到的文件和上级路径都是以"/"开头

(如URL:  http://blog.youkuaiyun.com/tenlay_li/article/details/79311857   ),

经过java.io.File修正路径"\"(如 C:\Windows\System32\drivers )

jsp页面:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>上传下载测试</title>
<meta name="decorator" content="default"/>
    <script type="text/javascript">
        $(document).ready(function() {
        });
        //上传
        function upload(obj) {
            var index = layer.open({
                title : "上传资料",
                type : 2,
                content : "${ctx}/wl/catalog/upload?id="+obj,
                area: ['50%', '50%']
            })
        }
        //下载
        function download(obj) {
            var url = "${ctx}/wl/catalog/download?id=" + obj;
            window.location.href=url;
        }
    </script>
</head>
<body>
    <form id="searchForm" modelAttribute="wlCatalog" action="${ctx}/wl/catalog/" method="post" enctype="multipart/form-data" class="breadcrumb form-search">
    <%--我这里是用onclick事件,也可以用a标签,不过a标签在鼠标放在上面就可以看到href的存放资源库地址,也可以说安全系数较低
    <a href="${ctx}/wl/catalog/download" class="btn btn-primary">下载</a>--%>
    </form>
    <button type="button" class="btn" onclick="upload('${catalog.id}')" >上传</button>
    <button type="button" class="btn" onclick="download('${catalog.id}')" >下载</button>
</body>
</html>

 

下面FileUtils写好了上传下载删除

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 java.io.RandomAccessFile;
import java.util.Enumeration;
import java.util.List;

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

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;

/**
  * 文件操作工具类
  * 实现文件的创建、删除、复制、压缩、解压以及目录的创建、删除、复制、压缩解压等功能
  */
public class FileUtils extends org.apache.commons.io.FileUtils {
    private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
    /**
     * 复制单个文件,如果目标文件存在,则不覆盖
     * @param srcFileName 待复制的文件名
     * @param descFileName 目标文件名
     * @return 如果复制成功,则返回true,否则返回false
     */
    public static boolean copyFile(String srcFileName, String descFileName) {
        return FileUtils.copyFileCover(srcFileName, descFileName, false);
    }
    /**
     * 复制单个文件
     * @param srcFileName 待复制的文件名
     * @param descFileName 目标文件名
     * @param coverlay 如果目标文件已存在,是否覆盖
     * @return 如果复制成功,则返回true,否则返回false
     */
    public static boolean copyFileCover(String srcFileName, String descFileName, boolean coverlay) {
        File srcFile = new File(srcFileName);
        // 判断源文件是否存在
        if (!srcFile.exists()) {
            logger.debug("复制文件失败,源文件 " + srcFileName + " 不存在!");
            return false;
        }
        // 判断源文件是否是合法的文件
        else if (!srcFile.isFile()) {
            logger.debug("复制文件失败," + srcFileName + " 不是一个文件!");
            return false;
        }
        File descFile = new File(descFileName);
        // 判断目标文件是否存在
        if (descFile.exists()) {
        // 如果目标文件存在,并且允许覆盖
            if (coverlay) {
                logger.debug("目标文件已存在,准备删除!");
                if (!FileUtils.delFile(descFileName)) {
                    logger.debug("删除目标文件 " + descFileName + " 失败!");
                    return false;
                }
            } else {
                logger.debug("复制文件失败,目标文件 " + descFileName + " 已存在!");
                return false;
            }
        } else {
            if (!descFile.getParentFile().exists()) {
        // 如果目标文件所在的目录不存在,则创建目录
                logger.debug("目标文件所在的目录不存在,创建目录!");
        // 创建目标文件所在的目录
                if (!descFile.getParentFile().mkdirs()) {
                    logger.debug("创建目标文件所在的目录失败!");
                    return false;
                }
            }
        }
        // 准备复制文件
        // 读取的位数
        int readByte = 0;
        InputStream ins = null;
        OutputStream outs = null;
        try {
            // 打开源文件
            ins = new FileInputStream(srcFile);
            // 打开目标文件的输出流
            outs = new FileOutputStream(descFile);
            byte[] buf = new byte[1024];
            // 一次读取1024个字节,当readByte为-1时表示文件已经读取完毕
            while ((readByte = ins.read(buf)) != -1) {
            // 将读取的字节流写入到输出流
                outs.write(buf, 0, readByte);
            }
            logger.debug("复制单个文件 " + srcFileName + " 到" + descFileName
                    + "成功!");
            return true;
        } catch (Exception e) {
            logger.debug("复制文件失败:" + e.getMessage());
            return false;
        } finally {
            // 关闭输入输出流,首先关闭输出流,然后再关闭输入流
            if (outs != null) {
                try {
                    outs.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值