国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

Nginx代理輸出縮放圖片的方法

2024-08-30 12:24:18
字體:
供稿:網(wǎng)友

本文實例為大家分享了Nginx代理輸出縮放圖片的具體代碼,供大家參考,具體內(nèi)容如下

nginx 配置文件:

# document ppt convert Configuration.upstream document.polyv.net { server 127.0.0.1:8080;}server { listen 80; server_name document.polyv.net; index index.html index.htm; charset utf-8; client_max_body_size 1000m; # ignore favicon.ico not exist. location = /favicon.ico {  log_not_found off;  access_log off; } # not allow to visit hidden files. location ~ //. {  deny all;  access_log off;  log_not_found off; } location / {  if ($request_filename ~* ^.*?/.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$) {   add_header Content-Disposition: 'attachment;';   add_header Content-Type: 'APPLICATION/OCTET-STREAM';  }  proxy_pass http://document.polyv.net;  proxy_set_header X-Real-IP $remote_addr;  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  proxy_set_header REQUEST_HOST $host;  # include proxy.conf;  charset UTF-8; } # user upload files location /images/ {   #expires 7d;  alias /data03/ovp/blobs/;   proxy_store on;   proxy_store_access user:rw group:rw all:rw;   proxy_set_header Accept-Encoding "";   if ( !-f $request_filename ) {    proxy_pass http://document.polyv.net;   } } location /blobs/ {   #expires 7d;  alias /data03/ovp/blobs/; }  location /preview/images/ {   #expires 7d;   alias /data03/ovp/blobs/;   proxy_store on;   proxy_store_access user:rw group:rw all:rw;   proxy_set_header Accept-Encoding "";   if ( !-f $request_filename ) {    proxy_pass http://document.polyv.net;   }  }}

代理輸出縮放圖片

package com.document.handle.controller;import java.io.BufferedInputStream;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.ServletRequestUtils;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import com.document.tool.ImageMagickUtils;import com.document.tool.SystemConfig;@Controllerpublic class ImageAgentController { private static final Logger LOG = LoggerFactory.getLogger(ImageAgentController.class); /**  * ppt預覽圖片代理輸出  * @throws IOException  */ @RequestMapping("/preview/images/{year}/{month}/{md5id}/{preview}/{filename}.{ext}") public void cropImage(@PathVariable String year, @PathVariable String month, @PathVariable String md5id,   @PathVariable String preview, @PathVariable String filename, @PathVariable String ext,   HttpServletRequest request, HttpServletResponse response) throws IOException {  // String rootDir = "/data03/ovp/blobs/";  String rootDir = SystemConfig.getBlobDirectory();  String oname = filename.substring(1, filename.length());// 原圖文件名  String dirString = rootDir + year + "/" + month + "/" + md5id + "/" + oname + "." + ext;  String targetFileString = rootDir + year + "/" + month + "/" + md5id + "/preview/" + filename + "." + ext;  //如果原圖存在  File originImage = new File(oname);  if(originImage.exists()){   LOG.info("corpImage..." + dirString + " -> " + targetFileString);   File newfile = new File(targetFileString);   String pathString = newfile.getParent();   LOG.info("pathString...{} {}", pathString);   File pathFile = new File(pathString);   if (!pathFile.exists()) {    LOG.info("---create file---");    pathFile.mkdirs();   }   boolean status = ImageMagickUtils.scale(dirString, targetFileString, 240, 180);   if (status) {    response.reset();    response.setContentType("image/" + ext);    java.io.InputStream in = new java.io.FileInputStream(targetFileString);    // FilenameUrlUtils.getImageFilename(targetFileString);    if (in != null) {     byte[] b = new byte[1024];     int len;     while ((len = in.read(b)) != -1) {      response.getOutputStream().write(b);     }     in.close();    }   }  }else{   LOG.info("原圖目錄不存在-preview:{}",dirString);   } } /**  * ppt固定尺寸圖片代理輸出  * @throws IOException  * http://document.polyv.net/images/2016/03/de37d2ceb11ac068c18c5e4428541075/jpg-3/1000x540.png  *  * http://document.polyv.net/images/2016/03/de37d2ceb11ac068c18c5e4428541075/jpg-3.png  */ @RequestMapping("/images/{year}/{month}/{md5id}/{filename}/{width}x{height}.{ext}") public void cropfixedImage(@PathVariable String year, @PathVariable String month, @PathVariable String md5id,   @PathVariable String filename, @PathVariable Integer width, @PathVariable Integer height, @PathVariable String ext,   HttpServletRequest request, HttpServletResponse response) throws IOException {  // String rootDir = "/data03/ovp/blobs/";  String rootDir = SystemConfig.getBlobDirectory();  //String oname = filename.substring(1, filename.length());// 原圖文件名  String dirString = rootDir + year + "/" + month + "/" + md5id + "/" + ( filename + "." + ext);  String targetFileString = rootDir + year + "/" + month + "/" + md5id + "/" + filename + "/" + (width + "x" + height + "." + ext);  //如果原圖存在  File originImage = new File(dirString);  if(originImage.exists()){   File targetFileStringFile = new File(targetFileString);   if(!targetFileStringFile.exists()){    LOG.info("corpImage..." + dirString + " -> " + targetFileString);    File newfile = new File(targetFileString);    String pathString = newfile.getParent();    LOG.info("pathString...{} {}", pathString);    File pathFile = new File(pathString);    if (!pathFile.exists()) {     LOG.info("---create file---");     pathFile.mkdirs();    }    ImageMagickUtils.resizeWH(dirString, targetFileString,width,height);   }   response.setContentType("image/" + ext);   java.io.InputStream in = null;   try{    in = new java.io.FileInputStream(targetFileString);    response.setContentLength(in.available());    byte[] buffer = new byte[1024];    int count = 0;    while ((count = in.read(buffer)) > 0) {     response.getOutputStream().write(buffer, 0, count);    }    response.flushBuffer();   }catch(Exception e){    e.printStackTrace();   }finally {    try {     in.close();    } catch (Exception e) {    }   }  }else{   LOG.info("原圖目錄不存在:{}",dirString);  } } /**  * 圖片下載  */ @RequestMapping("get/image/data") public void downloadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {   String filePath = ServletRequestUtils.getStringParameter(request, "filePath", ""); //圖片訪問路勁  String fileName = ServletRequestUtils.getStringParameter(request, "fileName", ""); //名稱  if(StringUtils.isNotBlank(filePath) || StringUtils.isNotBlank(fileName)){   String destUrl = filePath;   //LOG.info("--------------"+filePath);    String fileFormat=filePath.substring(filePath.lastIndexOf("."));   //String name=fileName.trim()+fileFormat;   String name=filePath.substring(filePath.lastIndexOf("/")+1, filePath.length());    //File f = new File(filePath);   //response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(f.getName(),"UTF-8"));    //LOG.info("--------------"+f.getName());   // 建立鏈接    URL url = new URL(destUrl);    HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();    // 連接指定的資源    httpUrl.connect();    // 獲取網(wǎng)絡輸入流    BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());    Integer lenf=httpUrl.getContentLength();   //String lenf=this.getFileLength(4189053, 7189053);   response.setContentType("application/x-msdownload");    response.setHeader("Content-Length", lenf.toString());//文件大小值5幾M   response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(name,"UTF-8"));   OutputStream out = response.getOutputStream();   byte[] buf = new byte[1024];    if (destUrl != null) {     BufferedInputStream br = bis;     int len = 0;     while ((len = br.read(buf)) > 0){      out.write(buf, 0, len);     }         br.close();    }    out.flush();    out.close();   } } }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 米易县| 雷山县| 鄄城县| 无为县| 汪清县| 武平县| 平度市| 高邑县| 东莞市| 金乡县| 金川县| 威远县| 自治县| 双流县| 左云县| 板桥市| 唐河县| 金湖县| 囊谦县| 南安市| 华宁县| 鹤岗市| 当阳市| 荆州市| 吉安县| 三亚市| 萍乡市| 镇赉县| 满洲里市| 盘锦市| 扬州市| 宣化县| 都安| 峨山| 水城县| 洮南市| 镇江市| 磐安县| 井陉县| 鸡西市| 台中县|