publicstaticStringfileUploadReturnPath(Fileuploads,StringuploadsFileName,Stringmodel){//1.文件上传保存路径StringbasePath=ServletActionContext.getServletContext().getRealP...
public static String fileUploadReturnPath(File uploads,
String uploadsFileName, String model) {
//1.文件上传保存路径
String basePath = ServletActionContext.getServletContext().getRealPath("/upload");
//2.按日期创建文件夹
String datePath = DateUtils.dateToStringByFile(new Date());
datePath+="/";
//3.上传文件后缀
String perfix = uploadsFileName.substring(uploadsFileName.lastIndexOf("."));
//4.文件名使用UUID保证唯一性
String fileName = UUID.randomUUID().toString()+perfix;
//5.如果日期文件夹不存在,创建文件夹
File dateFile = new File(basePath + datePath + model);
if (!dateFile.exists()) {
dateFile.mkdirs();
}
//6.文件上传
File destFile = new File(basePath + datePath + model + "/" + fileName);
//uploads:源文件 renameTo:剪切至 destFile:目标文件
uploads.renameTo(destFile);
return "/upload" + datePath + model + "/" + fileName;
}
/**File dateFile1 = new File(basePath + datePath + model);
dateFile1创建的文件夹路径:D:\apache-tomcat-7.0.70\webapps\elec\upload\2016\07\26用户管理
我想要[26\用户管理]这样结果.于是修改代码为如下:
File dateFile2 = new File(basePath + datePath + "/" + model);
dateFile2的结果://D:\apache-tomcat-7.0.70\webapps\elec\upload\2016\07\26\用户管理
但是创建的文件夹路径是对的了. 但文件上传功能却不好使了~ 我不知道哪里出了问题.
试了各种办法,都没用. 只有File dateFile1 = new File(basePath + datePath + model); 这样才好使.
但文件路径就成了2016\07\26用户管理
于是我用苯办法,在datePath后面加了一行datePath+="/";
此时 文件夹的创建路径为//D:\apache-tomcat-7.0.70\webapps\elec\upload\2016\07\26\用户管理
文件也可以正常上传了.
求 问题出在哪里
*/
展开