/**
* SpringBoot方式启动类
*
*/
@SpringBootApplication(scanBasePackages={"com.test"})
public class Application {
* SpringBoot方式启动类
*
*/
@SpringBootApplication(scanBasePackages={"com.test"})
public class Application {
protected final static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("Application is sussess!");
}
// 设置 ajax 跨域
@Bean
public WebMvcConfigurer coresConfigurer(){
return new WebMvcConfigurerAdapter() {
@SuppressWarnings("unused")
public void addCorsMapping(CorsRegistry registry){
registry.addMapping("/**").allowedOrigins("*");
}
};
}
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("20MB");
factory.setMaxRequestSize("20MB");
return factory.createMultipartConfig();
}
}
-----------------------------------------------------------------------------------
### 上传文件路径
uploadurl:
#linux
duty.uploadurl: /usr/local/dutymtfile
#window
#duty.uploadurl: D:/dutymtfile
uploadurl:
#linux
duty.uploadurl: /usr/local/dutymtfile
#window
#duty.uploadurl: D:/dutymtfile
-----------------------------------------------------------------------------------
@Controller
@RequestMapping("/honor")
public class ItmsDutyHonorController extends BaseController{
// 在配置文件,自动配置
@Value("${uploadurl.duty.uploadurl}")
private String uploadurl;
@RequestMapping(value="/addUpdateHonor",method=RequestMethod.POST)
@ResponseBody
public JsonResult addUpdateCriticism(@RequestParam(value="file",required=false) MultipartFile multipartFile , HttpServletRequest request){ //@ModelAttribute("ItmsDutyHonor") ItmsDutyHonor honor,
JsonResult jsonResult = new JsonResult();
// System.out.println("==="+multipartFile.getOriginalFilename());
// JSONObject jsonstr = new JSONObject();
String strData = request.getParameter("data");
ItmsDutyHonor honor =JSON.parseObject(strData, ItmsDutyHonor.class);
int i = 0;
try
{
if(null!=multipartFile && !multipartFile.isEmpty()){
Calendar date = Calendar.getInstance();
File file = new File(File.separator+date.get(Calendar.YEAR)+File.separator+(date.get(Calendar.MONTH)+1));
System.out.println(file);
if(!file.exists()){
file.mkdirs();
}
//上传文件路径
//String path = request.getSession().getServletContext().getRealPath("/mtfile/"+file);
//System.err.println(uploadurl);
String path = uploadurl+file;
//获取
String filename = multipartFile.getOriginalFilename();
System.out.println(multipartFile.getSize());
//自定义名字
Date nowtime = new Date();
String truefilename = String.valueOf(TimeUtil.getTimeStr(nowtime, TimeUtil.PATTERN_DATE_3))+"_"+filename;
File filepath = new File(path,truefilename);
if(!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
multipartFile.transferTo(new File(path+File.separator+truefilename));
honor.setMtfj("dutymtfile"+file+File.separator+truefilename);
i = honorService.addUpdateHonor(honor);
}else if(honor.getId()!=null){
i = honorService.addUpdateHonor(honor);
}
if (i > 0)
{
jsonResult.setCode("200");
jsonResult.setMessage("success");
}
else
{
jsonResult.setCode("500");
jsonResult.setMessage("fail");
}
}
catch(Exception ex)
{
ex.printStackTrace();
jsonResult.setCode("500");
jsonResult.setMessage("fail");
}
return jsonResult;
}
}
-----------------------------------------------------------------------------------
/**
*
* @Description : 下载附件;
* @Method_Name : downfile;
* @param request
* @throws Exception
* @return : void;
* @Creation Date : 2018年3月2日 下午4:57:56 ;
* @version : v1.00;
* @Author : tao;
* @Update Date :
* @Update Author : who;
*/
@RequestMapping("/downfile")
public void downfile(HttpServletRequest request) throws Exception{
//现将保存的参数 保存成文本形式 然后再下载到本地
String fname = URLDecoder.decode(request.getParameter("fileUrl"), "UTF-8");
//String docPath = request.getRealPath("") + File.separator + "dutymtfile" + File.separator + fname;
String docPath = uploadurl +File.separator + fname;
System.out.println("docPath:" + docPath);
@Controller
@RequestMapping("/honor")
public class ItmsDutyHonorController extends BaseController{
// 在配置文件,自动配置
@Value("${uploadurl.duty.uploadurl}")
private String uploadurl;
@RequestMapping(value="/addUpdateHonor",method=RequestMethod.POST)
@ResponseBody
public JsonResult addUpdateCriticism(@RequestParam(value="file",required=false) MultipartFile multipartFile , HttpServletRequest request){ //@ModelAttribute("ItmsDutyHonor") ItmsDutyHonor honor,
JsonResult jsonResult = new JsonResult();
// System.out.println("==="+multipartFile.getOriginalFilename());
// JSONObject jsonstr = new JSONObject();
String strData = request.getParameter("data");
ItmsDutyHonor honor =JSON.parseObject(strData, ItmsDutyHonor.class);
int i = 0;
try
{
if(null!=multipartFile && !multipartFile.isEmpty()){
Calendar date = Calendar.getInstance();
File file = new File(File.separator+date.get(Calendar.YEAR)+File.separator+(date.get(Calendar.MONTH)+1));
System.out.println(file);
if(!file.exists()){
file.mkdirs();
}
//上传文件路径
//String path = request.getSession().getServletContext().getRealPath("/mtfile/"+file);
//System.err.println(uploadurl);
String path = uploadurl+file;
//获取
String filename = multipartFile.getOriginalFilename();
System.out.println(multipartFile.getSize());
//自定义名字
Date nowtime = new Date();
String truefilename = String.valueOf(TimeUtil.getTimeStr(nowtime, TimeUtil.PATTERN_DATE_3))+"_"+filename;
File filepath = new File(path,truefilename);
if(!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
multipartFile.transferTo(new File(path+File.separator+truefilename));
honor.setMtfj("dutymtfile"+file+File.separator+truefilename);
i = honorService.addUpdateHonor(honor);
}else if(honor.getId()!=null){
i = honorService.addUpdateHonor(honor);
}
if (i > 0)
{
jsonResult.setCode("200");
jsonResult.setMessage("success");
}
else
{
jsonResult.setCode("500");
jsonResult.setMessage("fail");
}
}
catch(Exception ex)
{
ex.printStackTrace();
jsonResult.setCode("500");
jsonResult.setMessage("fail");
}
return jsonResult;
}
}
-----------------------------------------------------------------------------------
/**
*
* @Description : 下载附件;
* @Method_Name : downfile;
* @param request
* @throws Exception
* @return : void;
* @Creation Date : 2018年3月2日 下午4:57:56 ;
* @version : v1.00;
* @Author : tao;
* @Update Date :
* @Update Author : who;
*/
@RequestMapping("/downfile")
public void downfile(HttpServletRequest request) throws Exception{
//现将保存的参数 保存成文本形式 然后再下载到本地
String fname = URLDecoder.decode(request.getParameter("fileUrl"), "UTF-8");
//String docPath = request.getRealPath("") + File.separator + "dutymtfile" + File.separator + fname;
String docPath = uploadurl +File.separator + fname;
System.out.println("docPath:" + docPath);
FileInputStream in = null;
OutputStream outp = null;
try {
// 设置响应头和保存文件名
// response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fname, "UTF-8") + "\"");
outp = response.getOutputStream();
in = new FileInputStream(new File(docPath));
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0){
outp.write(b, 0, i);
}
outp.flush();
//out.clear();
//out = pageContext.pushBody();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in != null){
in.close();
}
if(outp != null){
outp.close();
}
OutputStream outp = null;
try {
// 设置响应头和保存文件名
// response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fname, "UTF-8") + "\"");
outp = response.getOutputStream();
in = new FileInputStream(new File(docPath));
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) > 0){
outp.write(b, 0, i);
}
outp.flush();
//out.clear();
//out = pageContext.pushBody();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in != null){
in.close();
}
if(outp != null){
outp.close();
}
}
}
}