| | |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.util.FileCopyUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.imageio.ImageIO; |
| | |
| | | // 获得文件的后缀 |
| | | String fileName = file.getOriginalFilename(); |
| | | String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1); |
| | | //限制文件格式为图片 |
| | | // 限制文件格式为图片 |
| | | if (!isImage(file.getInputStream())) { |
| | | Map<String,Object> map = new HashMap<String,Object>(); |
| | | map.put("code",1); // 1表示失败 |
| | | map.put("msg","文件格式不支持"); // 提示消息 |
| | | return map; |
| | | } |
| | | String newFileName = UUID.randomUUID().toString() + "." + fileExt; |
| | | if (isImage(file.getInputStream())) { |
| | | Thumbnails.of(file.getInputStream()) |
| | |
| | | // 图片质量压缩比例 从0-1,越接近1质量越好 |
| | | .outputQuality(0.9f) |
| | | .toOutputStream(new FileOutputStream(savePath + newFileName)); |
| | | } else { |
| | | File uploadedFile = new File(savePath, newFileName); |
| | | uploadedFile.setReadable(true, false); |
| | | uploadedFile.setExecutable(true, false); |
| | | uploadedFile.setWritable(true, false); |
| | | try { |
| | | //存文件 |
| | | FileCopyUtils.copy(file.getBytes(), uploadedFile); |
| | | file.transferTo(uploadedFile); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | // else { |
| | | // File uploadedFile = new File(savePath, newFileName); |
| | | // uploadedFile.setReadable(true, false); |
| | | // uploadedFile.setExecutable(true, false); |
| | | // uploadedFile.setWritable(true, false); |
| | | // try { |
| | | // //存文件 |
| | | // FileCopyUtils.copy(file.getBytes(), uploadedFile); |
| | | // file.transferTo(uploadedFile); |
| | | // } catch (IOException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | |
| | | String visitPath = (saveUrl + newFileName); |
| | | Map<String,Object> map = new HashMap<String,Object>(); |