Spring MVC 实现文件上传功能
2017-04-17 14:08
357 查看
@RequestMapping(value="/fileBusinessLicensePath", method=RequestMethod.POST)
@ResponseBody
public ResultInfo uploadBusinessLicensePath(@RequestParam("files[]") MultipartFile file){
Files files = new Files();
String names = file.getOriginalFilename();
files.setFileRealName(names);
names = new Date().getTime() + names.substring(names.indexOf("."));
String destFileName = "";
String destFilePath = ParamFileUtil.getConfigProperty("config","business_license") + names;
destFileName = ParamFileUtil.getConfigProperty("config","business_license_real") + System.getProperty("file.separator") + names;
if(createFile(destFileName,file)){
files.setFilePath(destFilePath);
} else {
return null;
}
Map<String,Object> result = new HashMap<>();
result.put("file", files);
return new ResultInfo(Constant.SUCCESS_CODE,Constant.SUCCESS,result);
}
/**
* 保存文件
* @param destFileName
* @return
*/
public static boolean createFile(String destFileName,MultipartFile files) {
File file = new File(destFileName);
if(file.exists()) {
System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
return false;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
System.out.println("目标文件所在目录不存在,准备创建它!");
if(!file.getParentFile().mkdirs()) {
System.out.println("创建目标文件所在目录失败!");
return false;
}
}
//创建目标文件
try {
if (file.createNewFile()) {
System.out.println("创建单个文件" + destFileName + "成功!");
files.transferTo(file);
return true;
} else {
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
return false;
}
}
//获取配置文件中的数据
public class ParamFileUtil {
/** Properties对象 */
private Properties prop;
public static Properties loadPropertiesFromBundlefile(String bundlefile) {
return loadPropertiesFromBundlefile(bundlefile, new Properties());
}
public static Properties loadPropertiesFromBundlefile(String bundlefile, Properties toprop) {
Properties properties = toprop;
try {
ResourceBundle rb = ResourceBundle.getBundle(bundlefile);
Enumeration enu = rb.getKeys();
while (enu.hasMoreElements()) {
String strPName = (String) enu.nextElement();
String strPValue = null;
if (strPName != null) {
strPValue = rb.getString(strPName);
}
if ((strPName != null) && (strPValue != null))
properties.setProperty(strPName, strPValue);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return properties;
}
public static String getConfigProperty(String propertyName,String propertyValue){
Properties prop = loadPropertiesFromBundlefile(propertyName);
return prop.getProperty(propertyValue);
}
}
@ResponseBody
public ResultInfo uploadBusinessLicensePath(@RequestParam("files[]") MultipartFile file){
Files files = new Files();
String names = file.getOriginalFilename();
files.setFileRealName(names);
names = new Date().getTime() + names.substring(names.indexOf("."));
String destFileName = "";
String destFilePath = ParamFileUtil.getConfigProperty("config","business_license") + names;
destFileName = ParamFileUtil.getConfigProperty("config","business_license_real") + System.getProperty("file.separator") + names;
if(createFile(destFileName,file)){
files.setFilePath(destFilePath);
} else {
return null;
}
Map<String,Object> result = new HashMap<>();
result.put("file", files);
return new ResultInfo(Constant.SUCCESS_CODE,Constant.SUCCESS,result);
}
/**
* 保存文件
* @param destFileName
* @return
*/
public static boolean createFile(String destFileName,MultipartFile files) {
File file = new File(destFileName);
if(file.exists()) {
System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
return false;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()) {
//如果目标文件所在的目录不存在,则创建父目录
System.out.println("目标文件所在目录不存在,准备创建它!");
if(!file.getParentFile().mkdirs()) {
System.out.println("创建目标文件所在目录失败!");
return false;
}
}
//创建目标文件
try {
if (file.createNewFile()) {
System.out.println("创建单个文件" + destFileName + "成功!");
files.transferTo(file);
return true;
} else {
System.out.println("创建单个文件" + destFileName + "失败!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
return false;
}
}
//获取配置文件中的数据
public class ParamFileUtil {
/** Properties对象 */
private Properties prop;
public static Properties loadPropertiesFromBundlefile(String bundlefile) {
return loadPropertiesFromBundlefile(bundlefile, new Properties());
}
public static Properties loadPropertiesFromBundlefile(String bundlefile, Properties toprop) {
Properties properties = toprop;
try {
ResourceBundle rb = ResourceBundle.getBundle(bundlefile);
Enumeration enu = rb.getKeys();
while (enu.hasMoreElements()) {
String strPName = (String) enu.nextElement();
String strPValue = null;
if (strPName != null) {
strPValue = rb.getString(strPName);
}
if ((strPName != null) && (strPValue != null))
properties.setProperty(strPName, strPValue);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return properties;
}
public static String getConfigProperty(String propertyName,String propertyValue){
Properties prop = loadPropertiesFromBundlefile(propertyName);
return prop.getProperty(propertyValue);
}
}
相关文章推荐
- spring mvc实现文件上传与下载功能
- Spring MVC实现文件的上传和下载的功能
- Spring MVC使用commons fileupload实现文件上传功能
- Spring MVC环境中的文件上传功能实现
- spring mvc 3.0 实现文件上传功能
- Spring MVC环境中文件上传功能的实现方法详解
- 在Spring mvc中配置upload,实现文件上传功能的两种方式
- 用Servlvet实现文件上传的功能
- 批量上传文件(功能实现)
- winform如何实现上传文件功能?
- Struts实现文件上传功能步骤
- 实现一个Ajax模式的文件上传功能有多难?
- 【SSI开发总结.8】Struts2中实现文件上传功能
- 实现和163邮箱添加附件相似的多上传文件功能
- Android下的应用编程——用HTTP协议实现文件上传功能
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名
- 利用FTPClient类实现文件的上传下载功能
- JAVA实现邮箱上传文件功能
- PHP 文件上传功能实现代码
- 实现一个Ajax模式的文件上传功能有多难?