Java SpringMVC毕业项目实战-学生信息管理系统

*/

@Controller

@RequestMapping(“/file”)

public class FileController {

@Resource(name = “fileServiceImpl”)

private FileService fileService;

@RequestMapping(“/upload”)

public String fileUpload(@RequestParam MultipartFile file, FileVO filevo, HttpServletRequest request) throws IOException {

//上传路径保存设置

// 把文件写到磁盘

String fileName = file.getOriginalFilename();

String[] str = fileName.split(“\.”);

String uuid = UUID.randomUUID().toString().replaceAll(“-”,“”);

String headPath = “E://upload/” + uuid+ “.”+str[str.length-1];

File dest = new File(headPath);

file.transferTo(dest);

filevo.setFileID(uuid);

filevo.setFilePath(headPath);

filevo.setUserID(null);

try {

fileService.save(filevo);

} catch (Exception e) {

e.printStackTrace();

}

return “redirect:/admin/showFile”;

}

@RequestMapping(“/downFile”)

public void down(HttpServletRequest request, HttpServletResponse response,String fileID) throws Exception{

FileVO fileVO = fileService.findById(fileID);

String fileName = fileVO.getFilePath();

String[] str = fileName.split(“\.”);

InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));

String filename = fileVO.getFileName()+“\.”+str[str.length-1];

filename = URLEncoder.encode(filename,“UTF-8”);

response.addHeader(“Content-Disposition”, “attachment;filename=” + filename);

response.setContentType(“multipart/form-data”);

BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

int len = 0;

while((len = bis.read()) != -1){

out.write(len);

out.flush();

}

out.close();

}

}

异常处理

package com.system.exception;

/**

  • 系统自定义异常类,针对预期异常,需要在程序中抛出此类的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值