layui文件上传到下载
-----------------------------后台代码------------------------------------
@RequestMapping(value = "upload")
@ResponseBody
public Map<String, Object> upload(
@RequestParam(value = "file", required = false) MultipartFile file,
String del, HttpServletRequest request, HttpSession session)
throws IllegalStateException, IOException {
Map<String, Object> map = new HashMap<String, Object>();
String myFileName = "";
File fileDir = null;
if (null != file) {
myFileName = file.getOriginalFilename();// 文件原名称
String Directory = ConfigUtil.getConfigByName("bathPath");
String Directory2 = ConfigUtil.getConfigByName("htps");
if (del != null && !"".equals(del)) {
// String keyword = new String(del.getBytes("ISO-8859-1"),
// "utf-8");
fileDir = new File(Directory + Directory2, del);
if (fileDir.exists()) {
fileDir.delete();
}
}
fileDir = new File(Directory + Directory2, myFileName);
if (!fileDir.exists()) { // 如果不存在 则创建
fileDir.mkdirs();
}
try {
file.transferTo(fileDir);
} catch (Exception e) {
}
} else {
}
map.put("name",
file.getOriginalFilename().substring(0,
myFileName.lastIndexOf(".")));
map.put("url", file.getOriginalFilename());
map.put("type",
file.getOriginalFilename().substring(
file.getOriginalFilename().lastIndexOf(".")));
return map;
}
---------------------------文件下载代码以及工具类--------------------------
public class ConfigUtil {
private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");
public static final String getConfigByName(String name) {
try {
return new String(bundle.getString(name).getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
System.out.println(ConfigUtil.getConfigByName("bathPath"));
}
}