拿走不谢
public String srcUrl(){
Map map = new HashMap();
Map mapSrc = new HashMap();
map.put("code", 1); //0表示上传成功
map.put("msg","上传失败"); //提示消息
mapSrc.put("src", "");
mapSrc.put("title", "");
map.put("data", mapSrc);
String rootPath = request.getSession().getServletContext().getContextPath();//根目录
try{
if(file==null){
responseJson(map);
return null;
}
long maxSize = (1024 * 1024) * 10;
long fileSize = file.length();
String size = this.getFileSize(fileSize);
if (fileSize > maxSize) {
map.put("msg","上传失败,资源信息最大上传10MB,当前文件" + size + "!"); //提示消息
responseJson(map);
return null;
}
String path="C:\\Users\\ASUS\\Desktop\\jszjh";
// String path=Constants.JSZJHFBGLPATH;
File p = new File(path);
if(!p.exists()) p.mkdirs();
String nbwjm = parseWjFile(file, fileFileName,path);
map.put("code", 0); //0表示上传成功
map.put("msg","上传成功"); //提示消息
mapSrc.put("src", rootPath+"/province/other/jszjh/jsp/pcCreate.jsp?pth="+path+ File.separator + nbwjm);
mapSrc.put("title", nbwjm);
map.put("data", mapSrc);
responseJson(map);
return null;
}catch(Exception x){
responseJson(map);
return null;
}
}
// 获取文件大小
public String getFileSize(long fileLength) {
String size = "0";
double len = fileLength;
DecimalFormat df = new DecimalFormat("0.0");
if (len > 0.1 * 1024 * 1024) {
size = df.format(len / (1024 * 1024)) + "MB";
} else if (len > 0.1 * 1024) {
size = df.format(len / 1024) + "KB";
} else {
size = len + "B";
}
return size;
}
/**
* 复制文件
*/
public String parseWjFile(File file, String wjmc, String filePath) {
// 新文件
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
String wjid = System.currentTimeMillis() + wjmc;
File uploadCopyWord = new File(filePath + File.separator + wjid);
FileUtil.copyFile(file, uploadCopyWord);
return wjid;
}