| | |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.imageio.stream.ImageOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.PrintWriter; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | public static FtpUtil ftp = null; |
| | | |
| | | @ApiOperation(value = "上传文件到FTP") |
| | | @RequestMapping(method= RequestMethod.POST,value="api/uploadFtp.do") |
| | | @RequestMapping(method= RequestMethod.POST,value="api/uploadFtp.do",headers = "content-type=multipart/form-data") |
| | | @ResponseBody |
| | | public void uploadFtp(HttpServletRequest request, HttpServletResponse response, String folderCode) throws Exception { |
| | | System.out.println("上传中"); |
| | |
| | | }else{ |
| | | ftp.connect(); |
| | | } |
| | | |
| | | CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); |
| | | if (multipartResovler.isMultipart(request)) { |
| | | MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; |
| | |
| | | String date = DateUtil.getNowShortDate(); |
| | | String fName = date+"/"+ UUID.randomUUID()+".jpg"; |
| | | String fileName = folder+fName; |
| | | // boolean r = ftp.uploadInputstream(compressImg(is),fileName); |
| | | boolean r = ftp.uploadInputstream(is,fileName); |
| | | if(r){ |
| | | context.put("success", true); |
| | |
| | | fileJSON.put("halfPath", fName); |
| | | fileJSON.put("prefixPath", prefixPath); |
| | | fileJSON.put("folder", folder); |
| | | fileJSON.put("addr", prefixPath+fileName); |
| | | context.put("data",fileJSON); |
| | | context.put("message","请求成功"); |
| | | writerJson(response, context); |
| | |
| | | } |
| | | |
| | | |
| | | public InputStream compressImg(InputStream is){ |
| | | try { |
| | | long originalFileSize = is.available(); |
| | | BufferedImage originalImage = ImageIO.read(is); |
| | | |
| | | double compressionRatio = Math.sqrt((double)originalFileSize / (200 * 1024.0)); |
| | | |
| | | if(compressionRatio >1){ |
| | | int compressedWidth = (int) (originalImage.getWidth() / compressionRatio); |
| | | int compressedHeight = (int) (originalImage.getHeight() / compressionRatio); |
| | | |
| | | BufferedImage compressedImage = new BufferedImage(compressedWidth, compressedHeight, BufferedImage.TYPE_INT_RGB); |
| | | Graphics2D graphics = compressedImage.createGraphics(); |
| | | graphics.drawImage(originalImage, 0, 0, compressedWidth, compressedHeight, null); |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | ImageOutputStream imOut = ImageIO.createImageOutputStream(byteArrayOutputStream); |
| | | ImageIO.write(compressedImage, "jpg", imOut); |
| | | InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
| | | graphics.dispose(); |
| | | return inputStream; |
| | | }else{ |
| | | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| | | ImageOutputStream imOut = ImageIO.createImageOutputStream(byteArrayOutputStream); |
| | | ImageIO.write(originalImage, "jpg", imOut); |
| | | InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); |
| | | return inputStream; |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return is; |
| | | } |
| | | |
| | | |
| | | public void upload(HttpServletRequest request, HttpServletResponse response, String folder, String bucketName, |
| | | String access_id, String access_key, String resourcePath, String endpoint) throws Exception { |