public void uploadFile(){json
UploadFile file = getFile("batfile", "p_w_picpaths");服務器
File source = file.getFile(); ide
String fileName = file.getFileName(); ui
String extension = fileName.substring(fileName.lastIndexOf(".")); url
String prefix; spa
if(".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension)){get
prefix = "p_w_picpaths"; string
fileName = WebUtils.uuid() + extension; it
}else{ io
prefix = "files";
}
JSONObject json = new JSONObject();
try {
FileInputStream fis = new FileInputStream(source);
File targetDir = new File(PropKit.use("config.properties").get("file.path") + prefix + "/");
if (!targetDir.exists()) {
targetDir.mkdirs();
}
File target = new File(targetDir, fileName);
if (!target.exists()) {
target.createNewFile();
}
FileOutputStream fos = new FileOutputStream(target);
byte[] bts = new byte[300];
while (fis.read(bts, 0, 300) != -1) {
fos.write(bts, 0, 300);
}
fos.close();
fis.close();
json.put("error", "success");
json.put("url", prefix + "/"+ fileName);
source.delete();
} catch (FileNotFoundException e) {
json.put("error", 1);
json.put("message", "上傳出現錯誤,請稍後再上傳");
} catch (IOException e) {
json.put("error", 1);
json.put("message", "文件寫入服務器出現錯誤,請稍後再上傳");
}
renderJson(PropKit.use("config.properties").get("file.path") + "files/"+ fileName);
}