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; @controller public?class?imageagentcontroller?{ ?private?static?final?logger?log?=?loggerfactory.getlogger(imageagentcontroller.class); ?/** ??*?ppt預(yù)覽圖片代理輸出 ??*?@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 ??*?https://cache.yisu.com/upload/ask_collection/20210726/113/19154.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)絡(luò)輸入流? ???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();? ??} ?}? }
圖片縮放的業(yè)務(wù)
package?com.document.tool; import?java.io.ioexception; import?javax.swing.imageicon; import?org.apache.commons.exec.commandline; import?org.apache.commons.exec.defaultexecuteresulthandler; import?org.apache.commons.exec.defaultexecutor; import?org.apache.commons.exec.executeexception; import?org.apache.commons.exec.executewatchdog; import?org.apache.commons.exec.executor; import?org.slf4j.logger; import?org.slf4j.loggerfactory; /** ?*?使用imagemagick對圖片文件進行處理的工具類。 ?*?@author?xingning?ou ?*/ public?abstract?class?imagemagickutils?{ ?private?static?final?string?executable_convert?=?"/usr/bin/convert";?//?convert命令 ?private?static?final?string?executable_composite?=?"/usr/bin/composite";?//?composite命令 ?private?static?final?long?execute_timeout?=?30?*?60?*?1000l;?//?30?minutes ?private?static?final?logger?log?=?loggerfactory.getlogger(imagemagickutils.class); ?/** ??*?執(zhí)行圖片處理的命令。 ??*?@param?cmdline?待執(zhí)行的命令 ??*?@return?exitvalue,一般等于0時表示正常運行結(jié)束 ??*?@throws?executeexception?命令執(zhí)行失敗時拋出此異常 ??*?@throws?ioexception?當(dāng)發(fā)生io錯誤時拋出此異常 ??*?@throws?interruptedexception?當(dāng)?shù)却惒椒祷亟Y(jié)果被中斷時拋出此異常 ??*/ ?public?static?int?executecommandline(commandline?cmdline)?throws?executeexception,?ioexception, ?interruptedexception?{ ??executor?executor?=?new?defaultexecutor(); ??executor.setexitvalue(0); ??//?kill?a?run-away?process?after?execute_time?milliseconds. ??executewatchdog?watchdog?=?new?executewatchdog(execute_timeout); ??executor.setwatchdog(watchdog); ??//?execute?the?print?job?asynchronously. ??defaultexecuteresulthandler?resulthandler?=?new?defaultexecuteresulthandler(); ??executor.execute(cmdline,?resulthandler); ??//?some?time?later?the?result?handler?callback?was?invoked. ??resulthandler.waitfor(); ??//?so?we?can?safely?request?the?exit?value. ??return?resulthandler.getexitvalue(); ?} ?/** ??*?按照高寬比例縮小圖片。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?width?圖片圖片的寬度 ??*?@param?height?目標(biāo)圖片的高度 ??*?@return?是否處理成功 ??*/ ?public?static?boolean?scale(string?src,?string?dst,?int?width,?int?height)?{ ??//?構(gòu)建命令 ??commandline?cmdline?=?new?commandline(executable_convert); ??cmdline.addargument(src); ??cmdline.addargument("-scale"); ??cmdline.addargument(width?+?"x"?+?height); ??cmdline.addargument(dst); ??try?{ ???executecommandline(cmdline); ???return?true; ??}?catch?(exception?e)?{ ???log.error("縮略圖片時發(fā)生異常,cause:?",?e); ???return?false; ??} ?} ?/** ??*?按照高寬比例縮小圖片。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?width?圖片圖片的寬度 ??*?@param?height?目標(biāo)圖片的高度 ??*?@return?是否處理成功 ??*/ ?public?static?boolean?thumbnail(string?src,?string?dst,?int?width,?int?height)?{ ??//?構(gòu)建命令 ??commandline?cmdline?=?new?commandline(executable_convert); ??cmdline.addargument(src); ??cmdline.addargument("-thumbnail"); ??cmdline.addargument(width?+?"x"?+?height); ??cmdline.addargument(dst); ??try?{ ???executecommandline(cmdline); ???return?true; ??}?catch?(exception?e)?{ ???log.error("縮略圖片時發(fā)生異常,cause:?",?e); ???return?false; ??} ?} ?/** ??*?添加圖片水印。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?logofile?水印圖片 ??*?@param?dissolve?和水印的融合度,0-100的數(shù)字 ??*?@param?gravity?疊放方向,east,west,north,south,northeast,northwest,southeast,southwest ??*?@return?是否處理成功 ??*/ ?public?static?boolean?drawlogo(string?src,?string?dst,?string?logofile,?int?dissolve,?string?gravity)?{ ??//?構(gòu)建命令 ??commandline?cmdline?=?new?commandline(executable_composite); ??cmdline.addargument("-dissolve"); ??cmdline.addargument(dissolve?+?"%"); ??cmdline.addargument("-gravity"); ??cmdline.addargument(gravity); ??cmdline.addargument(logofile); ??cmdline.addargument(src); ??cmdline.addargument(dst); ??try?{ ???executecommandline(cmdline); ???return?true; ??}?catch?(exception?e)?{ ???log.error("添加圖片水印時發(fā)生異常,cause:?",?e); ???return?false; ??} ?} ?/** ??*?添加圖片水印。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?logofile?水印圖片 ??*?@param?dissolve?和水印的融合度,0-100的數(shù)字 ??*?@param?x?水印距離左下角的距離 ??*?@param?y?水印距離右下角的距離 ??*?@return?是否處理成功 ??*/ ?public?static?boolean?drawlogo(string?src,?string?dst,?string?logofile,?int?dissolve,?int?x,?int?y)?{ ??imageicon?icon?=?new?imageicon(src); ??int?width?=?icon.geticonwidth();?//?源圖的寬 ??int?height?=?icon.geticonheight();?//?源圖的高 ??string?_x?=?string.valueof(width?-?x);?//?在x軸上水印圖片的左上頂點距離圖片左上角的距離 ??string?_y?=?string.valueof(height?-?y);?//?在y軸上水印圖片的左上頂點距離圖片左上角的距離 ??//?構(gòu)建命令 ??commandline?cmdline?=?new?commandline(executable_composite); ??cmdline.addargument("-dissolve"); ??cmdline.addargument(dissolve?+?"%"); ??cmdline.addargument("-geometry"); ??cmdline.addargument(_x?+?"+"?+?_y); ??cmdline.addargument(logofile); ??cmdline.addargument(src); ??cmdline.addargument(dst); ??try?{ ???executecommandline(cmdline); ???return?true; ??}?catch?(exception?e)?{ ???log.error("添加圖片水印時發(fā)生異常,cause:?",?e); ???return?false; ??} ?} ?/** ??*?裁剪圖片。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?width?目標(biāo)寬度 ??*?@param?height?目標(biāo)高度 ??*?@param?left?裁剪位置:距離左邊的像素 ??*?@param?top?裁剪位置:距離上邊的像素 ??*?@return?是否處理成功 ??*/ ?public?static?boolean?crop(string?src,?string?dst,?int?width,?int?height,?int?left,?int?top)?{ ??//?構(gòu)建命令 ??commandline?cmdline?=?new?commandline(executable_convert); ??cmdline.addargument(src); ??cmdline.addargument("-crop"); ??cmdline.addargument(width?+?"x"?+?height?+?"+"?+?left?+?"+"?+?top); ??cmdline.addargument(dst); ??try?{ ???executecommandline(cmdline); ???return?true; ??}?catch?(exception?e)?{ ???log.error("裁剪圖片時發(fā)生異常,cause:?",?e); ???return?false; ??} ?} ?/** ??*?獲取矩形的小圖。 ??*?@param?src?源圖片 ??*?@param?dst?目標(biāo)圖片 ??*?@param?width?目標(biāo)寬度 ??*?@param?height?目標(biāo)高度 ??*?@param?left?裁剪位置:距離左邊的像素 ??*?@param?top?裁剪位置:距離上邊的像素 ??*?@return?是否處理成功 ??*/ ?public?static?boolean?croprect(string?src,?string?dst,?int?width,?int?height,?int?left,?int?top)?{ ??imageicon?icon?=?new?imageicon(src); ??int?origwidth?=?icon.geticonwidth(); ??int?origheight?=?icon.geticonheight(); ??int[]?s?=?new?int[2]; ??if?(origwidth??scale)?{ ?????width?=?height?*?scale; ????}?else?if?((width?/?height)?<p>服務(wù)器上要安裝imagemagick。</p>
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END