Files.createFileIfNoExists(PATH)当PATH为项目根目录内的相对路径时可以正常创建文件夹,为本地磁盘的绝对路径时报错:java.io.IOException: 文件名、目录名或卷标语法不正确。
问答
关于Files.createFileIfNoExists(PATH)创建文件的问题
标签:
无
8 回复
public static String APP_UPLOAD_EXTERNAL = "G:\\temp";
private String filePathPrefix = "";
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
@SJson
public Object image(@RequestParam("Filedata") MultipartFile tf, HttpServletRequest request) {
String u = StringUtils.trimToEmpty(request.getParameter("u"));
System.out.print("temp:" + request.getSession().getServletContext().getRealPath("uploadTemp/"));
boolean isUrl = StringUtils.isBlank(u) || Boolean.parseBoolean(u) || u.equals("1");
if (!tf.isEmpty()) {
List<String> imageList = config.getList("upload.suffix.image", ",");
if (imageList.contains(tf.getOriginalFilename().substring(tf.getOriginalFilename().lastIndexOf(".") + 1))) {
filePathPrefix = Globals.APP_UPLOAD_EXTERNAL + "\\image\\";
return upload(tf, isUrl);
} else {
return Result.error("globals.upload.suffix");
}
}
return Result.error("globals.upload.fail");
}
private Result upload(MultipartFile tf, boolean isUrl) {
try {
String uploadMode = config.get("upload.mode", "file");
// String fileName = tf.getOriginalFilename();
String fileName = R.UU32() + tf.getOriginalFilename().substring(tf.getOriginalFilename().lastIndexOf("."));
String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
if ("fdfs".equalsIgnoreCase(uploadMode)) {
//如果使用文件服务器
byte[] fileBuff = tf.getBytes();
ClientGlobal.init(config);
//建立连接
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
//设置元信息
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", fileName);
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength", String.valueOf(tf.getSize()));
//上传文件
String fdfsFileId = client.upload_file1(fileBuff, fileExtName, metaList);
// 存进数据库
Sys_file sysFile = new Sys_file();
sysFile.setName(fileName);
sysFile.setSize(tf.getSize());
sysFile.setType(tf.getContentType());
sysFile.setUrl(config.get("fdfs.domain", "") + "/" + fdfsFileId);
sysFile.setId(R.UU32());
sysFile.setFdfs_file_id(fdfsFileId);
sysFile = sysFileService.insert(sysFile);
if (isUrl) {
return Result.success("globals.upload.success", sysFile.getUrl());
} else {
return Result.success("globals.upload.success", sysFile);
}
} else {
//String p = Globals.APP_ROOT;
String fileId = R.UU32();
if (filePathPrefix.equals("")) {
filePathPrefix = Globals.APP_UPLOAD_PATH + "/file/";
}
String f = filePathPrefix + DateUtil.format(new Date(), "yyyyMMdd") + "\\" + fileId + "." + fileExtName;
System.out.print("f:" + f);
tf.transferTo(Files.createFileIfNoExists(f));
// 存进数据库
Sys_file sysFile = new Sys_file();
sysFile.setName(fileName);
sysFile.setSize(tf.getSize());
sysFile.setType(tf.getContentType());
sysFile.setUrl(Globals.APP_BASE + "/open/file/download/" + fileId);
sysFile.setId(fileId);
sysFile.setPath(f);
sysFile = sysFileService.insert(sysFile);
if (isUrl) {
return Result.success("globals.upload.success", sysFile.getUrl());
} else {
sysFile.setPath("");
return Result.success("globals.upload.success", sysFile);
}
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("globals.upload.error");
}
}
String fileId = R.UU32();
if (filePathPrefix.equals("")) {
filePathPrefix = Globals.APP_UPLOAD_PATH + "/file/";
}
String f = filePathPrefix + DateUtil.format(new Date(), "yyyyMMdd") + "\\" + fileId + "." + fileExtName;
// 加上这一句
new File(f).create();
// 换行并打印
System.out.println();
System.out.println("Path=[" + f + "]");
tf.transferTo(Files.createFileIfNoExists(f));
看看输出什么
//String p = Globals.APP_ROOT;
String fileId = R.UU32();
if (filePathPrefix.equals("")) {
filePathPrefix = Globals.APP_UPLOAD_PATH + "/file/";
}
String f = filePathPrefix + DateUtil.format(new Date(), "yyyyMMdd") + "\\" + fileId + "." + fileExtName;
new File(f).createNewFile();
// 换行并打印
System.out.println();
System.out.println("Path=[" + f + "]");
tf.transferTo(Files.createFileIfNoExists(f));
输出:Path=[G:\temp\image\20180906\7irm15o2foh90pfl91ae0nchea.jpg]
添加回复
请先登陆