“src”: “图片路径”,
“title”: “图片名称” //可选
}
}
所以后台返回需要返回这几个参数
//上传
@RequestMapping(value=“/upload”)
@ResponseBody
public Object upload(MultipartFile file) {
String filename = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString();
//上传的方法在Service层写的,这里直接调用的
boolean boole = pService.savefile(file, uuid);
if (boole) {
Map<String,Object> map = new HashMap<String,Object>();
Map<String,Object> map2 = new HashMap<String,Object>();
map.put(“code”, 0); //0表示上传成功
map.put(“msg”,“上传成功”); //提示消息
//src返回图片上传成功后的下载路径,这里直接给绝对路径
map2.put(“src”, “http://localhost/layUITextarea/download?uuid=”+uuid);
map2.put(“title”, filename);
map.put(“data”, map2);
return map;
} else {
return new AjaxResult(true, file.getOriginalFilename());
}
}
//下载方法
@RequestMapping(value = “/download”)
@ResponseBody
private void download(String uuid, HttpServletRequest request, HttpServletResponse response) {
fileService.download(uuid, request, response);
}
Service层上传下载部分代码。
@Service
public class FileService extends CommonService<FileImg, Integer> {
@Autowired
private FileRepository fileRepository;
// 图片存放位置
private final static String IMAGEPATH=“e:\layui\image”;
//保存图片
@Transactional
public boolean savefile(MultipartFile file, String uuid){
try{
File path = path(file.getContentType());
String filename = file.getOriginalFilename();
FileImg fileEntity = new FileImg();
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date=new Date();
fileEntity.setFileName(filename);
fileEntity.setUuid(uuid);
String storeaddress = path.getAbsolutePath();
fileEntity.setStoreaddress(storeaddress);
File saveFile = new File(path,uuid);
try {
fileRepository.save(fileEntity);
file.transferTo(saveFile);
return true;
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
return false;
}
}catch (Exception e){
System.out.println(“图片保存异常”);
return false;
}
}
//图片地址是否存在
private File path(String filename) {
File pat=new File(“e:\layui”);
File path = new File(FileService.IMAGEPATH);
if(!pat.isDirectory()) {
pat.mkdir();
}
if(!path.isDirectory()) {
path.mkdir();
}
return path;
}
/**
-
下载
-
@param uuid
-
@param request
-
@param response
*/
public void download(String uuid, HttpServletRequest request, HttpServletResponse response) {
FileImg fileentity = fileRepository.findByUuid(uuid);
String filename = fileentity.getFileName();
filename = getStr(request, filename);
File file = new File(fileentity.getStoreaddress(), uuid);
if(file.exists()) {
FileInputStream fis;
try {
fis = new FileInputStream(file);
response.setContentType(“application/x-msdownload”);
response.addHeader(“Content-Disposition”, “attachment; filename=” + filename );
ServletOutputStream out = response.getOutputStream();
byte[] buf=new byte[2048];
int n=0;
while((n=fis.read(buf))!=-1){
out.write(buf, 0, n);
}
fis.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getStr(HttpServletRequest request, String fileName) {
String downloadFileName = null;
String agent = request.getHeader(“USER-AGENT”);
try {
if(agent != null && agent.toLowerCase().indexOf(“firefox”) > 0){
//downloadFileName = “=?UTF-8?B?” + (new String(Base64Utils.encode(fileName.getBytes(“UTF-8”)))) + “?=”;
//设置字符集
downloadFileName = “=?UTF-8?B?” + Base64Utils.encodeToString(fileName.getBytes(“UTF-8”)) + “?=”;
}else{
downloadFileName = java.net.URLEncoder.encode(fileName, “UTF-8”);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downloadFileName;
}