Tomcat文件上传时,绝对路径的存储中缺少分割符,导致文件下载时(系统找不到指定的文件)

    Part resumePart = request.getPart("resume");
    String realpath = request.getServletContext().getRealPath("");

    InputStream src = resumePart.getInputStream();
    String originalName = resumePart.getSubmittedFileName();
    String affix = originalName.substring(originalName.lastIndexOf("."));

    String newname = System.currentTimeMillis()+affix;
    String downloadPath = realpath+fileroot+"\\"+newname;
    OutputStream dest = new FileOutputStream(downloadPath);

    byte[] bs = new byte[1024];
    int size = 0;
    while((size = src.read(bs))>0){
        dest.write(bs, 0, size);
    }
    src.close();
    dest.close();
    
    sta.setResumepath(downloadPath);
    sta.setOriginalname(originalName);

G:eeweb-1914010118-wangboWebContentposi1638530953857.bat (系统找不到指定的文件。)

我的解答思路和尝试过的方法

正确的存储路径应该是

G:\ee\web-1914010118-wangbo\WebContent\posi\1638530953857.bat

是语句存入数据库中选择了,直接插入SQL语句,导致了语句中的\被当做注释符,消掉了。

File.separator自动获取对应系统的文件分隔符。

 String downloadPath = realpath+fileroot+"\\"+newname;
改为
 String downloadPath = realpath+fileroot+File.separator+newname;

是语句存入数据库中选择了,直接插入SQL语句,导致了语句中的\被当做注释符,消掉了。