图片的获取和上传
package com.jq.nfzl.system.controller; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.jq.nfzl.web.entity.NfAttachment; import net.coobird.thumbnailator.Thumbnails; @Controller public class ImgController { @Value("${file.savePath}") private String dir; // @Autowired // private NfInfoMapper m; // // @Autowired // private NfAttachmentDao n; /** * 图片获取 * @param response * @param path * @throws FileNotFoundException * @throws IOException */ @RequestMapping("/img") public void getImg(HttpServletResponse response, String path) throws FileNotFoundException, IOException { try { if (StringUtils.isEmpty(path) || path.indexOf("../") > -1) { response.sendRedirect("/static/img/noImg.jpg"); return; } BufferedImage bi = null; if("D:".equals(path.substring(0, 2))){ bi = ImageIO.read(new File(path)); }else{ bi = ImageIO.read(new File(dir + path)); } bi = Thumbnails.of(bi).size(800, 600).asBufferedImage(); String ext = path.substring(path.lastIndexOf(".") + 1); if ("jpg".equalsIgnoreCase(ext) || "jpeg".equalsIgnoreCase(ext)) { response.setContentType("image/jpg"); ImageIO.write(bi, "jpg", response.getOutputStream()); } else if ("png".equalsIgnoreCase(ext)) { response.setContentType("image/png"); ImageIO.write(bi, "png", response.getOutputStream()); } else if ("gif".equalsIgnoreCase(ext)) { response.setContentType("image/gif"); ImageIO.write(bi, "gif", response.getOutputStream()); } else { response.sendRedirect("/static/img/noImg.jpg"); } } catch (IOException e) { response.sendRedirect("/static/img/noImg.jpg"); } } /** * 图片上传 * @param files * @return * @throws IllegalStateException * @throws IOException */ @RequestMapping("/uploadImg") @ResponseBody public List<NfAttachment> uploadImg(HttpServletRequest request, HttpServletResponse response, MultipartFile[] files) throws IllegalStateException, IOException { response.setHeader("Access-Control-Allow-Origin", "*"); List<NfAttachment> result = new ArrayList<>(); String dataDir = new SimpleDateFormat("yyyyMMdd").format(new Date()); File path = new File(dir + dataDir + "/"); if (!path.exists()) { path.mkdirs(); } for (MultipartFile file : files) { NfAttachment attachment = new NfAttachment(); String originalFilename = file.getOriginalFilename(); String filePath = dataDir + "/" + UUID.randomUUID().toString().replaceAll("-", "") + "." + originalFilename.substring(originalFilename.lastIndexOf(".") + 1); file.transferTo(new File(dir + filePath)); attachment.setAttachmentName(originalFilename); attachment.setAttachmentPath(filePath); result.add(attachment); } return result; } // @RequestMapping("/testImg") // @ResponseBody // public Result test() throws IOException { // String ddd = "D:\\nfzlFiles\\"; // String prev = "20180323"; // File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹"); // for (File dir : file.listFiles()) { // String cm = dir.getName(); // for (File f : dir.listFiles()) { // String u = f.getName().substring(0, f.getName().lastIndexOf(".")).replaceAll("\\d", ""); // List<NongfangInfo> infos = m.getNf(u, cm); // if (infos.size() == 0) { // // } else if (infos.size() > 1) { // System.out.println(); // } else { // String fileName = UUID.randomUUID().toString().replaceAll("-", "") + f.getName().substring(f.getName().lastIndexOf(".")); // File tmp = new File(ddd + prev + "/" + fileName); // copyFile(f, tmp); // NongfangInfo info = infos.get(0); // NfAttachment attachment = new NfAttachment(); // attachment.setAttachmentName(f.getName()); // attachment.setAttachmentPath(prev + "/" + fileName); // attachment.setAttachmentType(3); // attachment.setRelId(info.getId()); // n.insertNfAttachment(attachment); // f.delete(); // } // } // } // return Result.SUCCESS; // } // // public static void main(String[] args) { // File file = new File("C:\\Users\\Administrator\\Desktop\\新建文件夹"); // for (File dir : file.listFiles()) { // String cm = dir.getName(); // for (File f : dir.listFiles()) { // System.out.println(f.getName().substring(0, f.getName().lastIndexOf(".")).replaceAll("\\d", "")); // } // } // } // // public void copyFile(File fromFile,File toFile) throws IOException{ // FileInputStream ins = new FileInputStream(fromFile); // FileOutputStream out = new FileOutputStream(toFile); // byte[] b = new byte[10*1024]; // int n=0; // while((n=ins.read(b))!=-1){ // out.write(b, 0, n); // } // // ins.close(); // out.close(); // } }