package cn.lonwin.rcs.rcscommon.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.*;
public class UploadUtils {
@Value("${upload.folder}")
private static String uploadPath;
public static Map<String, Object> reportData(MultipartFile file,String module,Long picId){
Map<String, Object> resultMap = new HashMap<String, Object>();
String mon;
String day1;
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR); //當前年
int month = (now.get(Calendar.MONTH) + 1) ; //當前月
int day = now.get(Calendar.DAY_OF_MONTH); //當前日
if(month<10){
mon = "0"+month;
}else{
mon=month+"";
}
if(day<10){
day1 = "0"+day;
}else{
day1 = ""+day;
}
String wjj = year+mon+day1;//文件夾名稱
Map<String, Object> pointMap = new HashMap<String, Object>();
Map<String, Object> mediaMap = new HashMap<String, Object>();
try {
String media_id = "";
String filePath = "D:/xxxxxxx/"+module+"/"+wjj; //文件路徑
System.out.println(uploadPath);
// String filePath = uploadPath + "/" +module+"/"+wjj; //文件路徑
String filePath_sjk = "/" +module+"/"+wjj ; //存到數據庫中的路徑名
List<Map<String,Object>> mediaList = new ArrayList<Map<String,Object>>();
String filename = file.getOriginalFilename();
String fileType =filename.substring(filename.lastIndexOf(".")+1);
String newName = String.valueOf(picId) + "."+fileType;
String path = "/" +module+"/"+wjj + "/" + newName;
upFile(file.getInputStream(),newName,filePath);
resultMap.put("pictureType", fileType);//文件擴展名
resultMap.put("path", path);
resultMap.put("pictureNm", filename);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return resultMap;
}
public static void upFile(InputStream is, String fileName, String filePath) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
File f = new File(filePath + "/" + fileName);
try {
bis = new BufferedInputStream(is);
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
byte[] bt = new byte[4096];
int len;
while ((len = bis.read(bt)) > 0) {
bos.write(bt, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bos) {
bos.close();
bos = null;
}
if (null != fos) {
fos.close();
fos = null;
}
if (null != is) {
is.close();
is = null;
}
if (null != bis) {
bis.close();
bis = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Java上傳工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.