java 图片上传后台代码,把图片保存到服务器本地盘,并且按照一天一个文件夹分开存放,易于管理,不说了直接上代码:
@Controller
@RequestMapping("/oa")
public class SavePicController extends BaseController {
@RequestMapping("/savepic")
@ResponseBody
public String savePicOnService(MultipartFile file, HttpServletRequest request) {
Date curDate = new Date();
SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String picDir = OaConstant.picDir.picloc + ymdFormat.format(curDate);
File picDirGsPic = new File(OaConstant.picDir.picloc);
if(!picDirGsPic.exists()) {
picDirGsPic.mkdir();
}
File picDirF = new File(picDir);
if(!picDirF.exists()) {
picDirF.mkdir();
}
String picPath = picDir+"\\" + dateFormat.format(new Date()) + ".jpg";
FileOutputStream fout;
try {
fout = new FileOutputStream(picPath);
fout.write(file.getBytes());
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
JSONObject object = new JSONObject();
object.put("code","200");
object.put("data",picPath);
return object.toJSONString();
}
}