2015-05-28 93 views
0

我無法使用spring MVC在瀏覽器中彈出下載窗口。我正在從Amazon S3獲取文件,並試圖在瀏覽器中使用REST調用對控制器進行調用,該調用會提示用戶下載PDF。當我點擊終端時,窗口沒有彈出,我在響應中得到了一些隨機字符,所以我想也許我沒有正確使用輸入/輸出流。我究竟做錯了什麼?我還應該提到我們在前端使用Backbone JS,我知道它使用jQuerys ajax來進行REST調用。這會有什麼影響嗎?觸發器瀏覽器PDF下載Spring MVC

控制器:

@ResponseStatus(HttpStatus.OK) 
    @ResponseBody 
    @RequestMapping(value = "/email/download/{pdfId}", method = RequestMethod.GET) 
    public void downloadStatusReport(@PathVariable("pdfId") String pdfId, HttpServletResponse response) { 
     response.setContentType("application/pdf"); 
     response.setHeader("Content-Disposition", "attachment; filename=\"" + "RemediationStatusReport.pdf\""); 
     response.setHeader("Content-Transfer-Encoding", "binary"); 
     response.setHeader("X-Frame-Options", "ALLOWALL"); 
     s3FileHandler.downloadPdfFileFromS3(pdfId, response); 
    } 

方法調用,從S3下載文件併發送回一個響應:

public void downloadPdfFileFromS3(String id, HttpServletResponse response) { 
     S3Object s3Object = client.getObject(new GetObjectRequest(S3_BUCKET_NAME, id)); 
     InputStream inputStream = s3Object.getObjectContent(); 
     LOGGER.info("PDF successfully downloaded from S3"); 
     try { 
      IOUtils.copy(inputStream, response.getOutputStream()); 
      response.flushBuffer(); 
     } catch (IOException io) { 
      LOGGER.error("IO Exception when processing PDF", io); 
     } 
    } 

一些反應:

%PDF-1.4 
%âãÏÓ 
2 0 obj 
<</Type/XObject/ColorSpace/DeviceGray/Subtype/Image/BitsPerComponent 8/Width 840/Length 8784/Height 141/Filter/FlateDecode>>stream 
xíyTÅñÇgO.å[email protected]!à 
FC¼¢"r,"&A7¢ á'7b4A5Ê­"7xSÈ" 7˲°L=æxÝõ­ê~o_òý{º^uÏôt¿útWù|:öÛÐé½>5øæË´rCÕß}üò|«Oîj>jÛÍ÷ÀMû¶~ا¨Û¿&ñþÇe«ÆëqS©Ó3ò$O5Õ¥SuN:­RÜo­zûÙ¤nï˾ºà¾mûÕ'±6hûìbäË;é§.§l^æS÷Y31½Q²Àë&O5ÔÆ÷Õx°îâ©ÍãÛ±è¢NlÂý:aóÉ_·:hÙ+h ÿÌÐCݼÑ7Æþç®zý¥Wùñc´cRñG(qº+.æ¼ÑÚ:ÀqjÍG7þÊú m*tÓ97©§ï±âÿÁùVd¹]5ÆÉïÏÖÞÎ/y·KÞùTe+êÆrizgÃ× 
\´óÛH|©ìË>o¨nÿµ7¼ûr¦Rí­¶,×U%c¡ó\ô0û³]?©ª¼½Õ¦&}¿Köï»Ù¾´v·Ó.P·ÿÀf~ý°9¾[j½Ë棥QßôU)ÞBÔI#gR7ûî·÷èrãݱ3Z×ëÌY´ÕêæUWXîÁ^eQ×<jùÙ2M«g6»ìàOù§UíÕ<_òFY|ö<wÆ+_ðy °ÙMÝü-Ö{plx%Ð÷i§¬?\¤éF»»¤Unû·°%öáú²ÐÞÃ[ÿäÆ`êÀ¿Çd¡mk¡n?(Û.À3ùgèào¨¿<|Ü?lçzò=ëî~ØïY÷åÊXPXZ?ôvw%!ÖÔ¦¦ÒÍ]\3/ü;9EèVƲÙvµ§Û£° 
ýp+ìK§XRXV4BÜïwÈ­ç³5\Èzâ^Þ|övÌ [email protected]ǶõpËÖI'ú¢Î< 
XsÂ*5¹ 
àÿòtFeñÄiù·©ºÄ=.ø§Ài¶îRNп¶Ü§°ÎÊlFû_åG`*««Ë&1ÕÉúDºKâÅ 
[$Ö 
¥ÑЩµhKÞPXG=uÙû]õ'ÉÞÎM 
¡M(?ÈÐF[$Ö- 
®}_Zn¤-yFa4ïB²ó]v¥©ìs8qw©N/áz§SéÇENØ"±.RØ0A©o £°QÚÚêBmnûÀÑä¥.T®SØPÅôN§&§E>Ø"±®RØPí¾vf"`Ç[ 
©½]>taÅHîSØPý3Ï;.xWè%ë. 
ÕâK)_gÒV<§°º`Iß{à.æDJ>æ©w»¬Ýðë$uÁuÂhEÙ¯.ÄÂFH%*ÔÀHÞPØbY{Eª%^G-X×)l±r»¾tÚA  
×ô¹7n âL¤xö5Cê;§Qéþb[Ù[[email protected]íìJ0 
1zxBpÍÁÃßQØ"m+ 
;§QÃÃr¬X/(l±é(ìXQØ0é¯üõd½£°:ú¦QÅÑ,¶àG¶P7ÖùPØ/bGaC´ÿjí ¦°Éß="ä!-©VH¬7¶HÚ ¡°3bHaCYO;ª%mAò§aÉK 
[¨£B®éTc»î2vÀ3 
[¨Ï£q¡°è> 
Ñ\íá°[email protected]<Õvì`§¶P_B®ixÑómØZ^QØBéÂ3ý½º¹6DCtçðÒD³¡W$o)lö¶C\ÓèRÑÁºbÍ6|þµòÂèmõgÂ^«î'¶X­tã:긧¾ºÙÙyLa:>ðL£s1tÀílc 8ú¡ÒÂ.9-Rf͸¦÷½ÕQ`gç5-Ð÷´g:µ2½óeĶ2=¬ô¡°cbOa4X·³3zóuEÀΡ°§>¡Õss2w;ëitþ;²ñ(9MÍ×+Çlðë+3¡ÍëçªA(lOu_öû1Q?'g-_´á¯j©ÙG0øÃÈgô¿I¦þêWÍÓèBa]EY9£Kû£PïL*eaKeLbó%ý¨;7ÛcÜýL±C(¬fîLèÿSU¸¢óȵ'ñN5G¯¤<ù[ybh¹Ú¬»;°\ 
{LEwá[email protected]¾fÐåºÀ§hdLbÛG*'#½Ú¡N 
ÕdPX-)¸àѯrÁNèv5dc׺ÁÔú/á/ÔPØ_áîÔBAAä»ô£à3t2&±½ÆLµDÙ£êÉÂÎÝèD«©,ð³\óÙØ]D¸ÃÕKú7îÃDF`ÂægøSv6[ä]n¬ÛÉ¢ÚÕUî@@åÆÑÏØ®ûÓ 
UW1§G*ÚRØetó5tr×+>á|1±6v»íVó¦íÄPØãw²< 
£<ÐPüÑé÷Wéóª5~ÆÚ#q¡Jf¥_h0¤°ô9ÉP¤Ð+nA¢7v-gïFî­kkÛ'q¶¬º,*ÎEq7y[_¥¶yZHBa?Äø¼ÈRØí½#Wßô×,HôÆnåu«I¿õ[email protected]útÉ`Å»T`æç|VMè@PHÈpÃÞµÀ+*`×ÄÂ"<ÊÛFç4!;ÅÞf­^¾.Í!ÝÕ¾!Å5¥û{êC¦SÐÎN:JA¼O'ÓÊrhPØ,VZ")æÑöºùÏðöáä[email protected]üjk9Ì0Xê¤xàìÿǸ^]¢tÕöSkêWe9G(ì¿YÚÔdóM<ä~{Oõ®¥ÏÁË©Ãi¤½CWÙl¶Rr!%¼E·(l* ýó¨Â(ÚôÑ'¡w¨·D¡°ÓCTí}Ú¢ ½B·tÂ:hXìT¥ ê黶+DðîU/HÑD)l±Ê¾@[Σ[email protected]?óù®¾ÊE" 
PXf}´EçytáÇtKW)l¤þ^®m£´Fº¼Ç. 
×ü@=ªÒ­9¶@IwÓfeó¨ÚÛ´åwø|©à_ÃC¡°Cx&¦-:Ï£k?C·)l*.,úu»dâ´ Uo(CßqíéÆ, 
{óÉX7ãÌçR¢²ÓÄ"vßx6Oßú¸Oa×òJlvÀn-Wo|õù·vÛ-·Ð<Õ_µ Å?G7æQØ ® ÍæQ} ôæüU÷*ð @ýyòGÎñ:áRØÌî(:ÐJ¯jÚÙ9ìµÚB¯oTó(8 
Ǥ°¹µJÛÍy!ÿ£-éOXÂÎFkH:ì̦öqæ;[email protected]´ÎèJ 
ÔKôBGÔô~õÆ. Øp)¬GsAó[k{îßÒ5 ±Ç´ EtGM4z¦¯®Y ?AÁï5×kcv)ì$8碤+öJ?IÛÌ#$È°7=øÙ@ù{Ýe2½ 
U- 
QmÓñ|(§.lAúZI¥êiß°R¹îèT 
èg=ɦ°>_ùé´]ÁyïÛÎ/ø,2å$¢°x6AsGPع©¼¾øÀ)ï2e{Ý~Â.íʸÑë|`¡°ÇÙÖç« 
ûå'h¨¹¶º­(Bb_âw-(ûöÌN?ãÔÐ 
;AaTJ®N¤Gv)ì\Æm 
Ŧ°¾¸æ´Yþ}ØRÐVóg«$Ö 
ëë\(uºÇPØMÓq½Ùþ¼u<TJÕ\°{ÄJ~ø¡ð([email protected](ì 
¾gÈU¾#ì=ÕeÀFa}ñ 
¤ÀXÂ`ڢס°oRØë} bH#+nY¥°Ïs.Äíd¡°yøUv"Èi£ ùü$úóRëEîM8ë 
û¼zSQØËy}ÉiªLmù'õ »lÍ 
@¾PxÓBasº°®MüÄ}Ulý$ôDn:ýy)E(ì 
±qrmÂ1¡*Ba5sÚ 
ÔÀ1uÀN¬RØ0<ÊWÍÖptæ¤:pÉvgض§5àĺ@aÛ#é·Âݯ¹Àá 
ØmUYÓÌ£QçHÜQ;¢Z¤ÝÎv¬<RìcäI­tÀèÔ°&wÁÄ$Ö>®ç:½î#v¦+6_× 
ûÓkÂ&3¿m¢°s¸aiN¸¯Gõ2iÂÍ5>BPØ<^¾VdWçüzPØqj 
[Ú 
/(ò­[email protected]ªÈgÝÏÌ`m¢°3ýJ¸zmcRØrkÌHTøµËH¬} 
Ûú6'8m¯ 
ûGõ]¢°ùBR 
æU 
»Ã-ÚDaY}ºA~å0ËM´bQo\HÁ>mPØåcOØ4r®PX;¬»;6_AÌ#¶»^M(Mf¹U¬Iöo[wúÙu½£©[$¶] 
[æi,Åc=>Âj*²þÅ% 
ryB0lRØ ì²¸Ñ'¬SØø&hñøµ¼ÞÞ\Õª[$¶'àNa eJKBV¶2ß([email protected]½öѶó¨5Zø¿]F¤ Û6¾ÁÔf½òÔu´É-¿jV ±ä[åÆÐva 
[ý4îzØñ>¥!­²n.¢°uþ6.Gc 
±<û ½nuáÁ£¢rÚ¥°¥º#çK 
Ä+W^ 
H\dÈW:ÝNBbSgÓv1 
[ºÍ»x¸èóLRØö®QXkóÕcʳ{EÃ/G·$Ò- 
xU»ûF°µt>í§ÒåÀök§wÏÖ/{-p¹¤°U¯¹õñYÀ{V±JRØ®QX_¬çÑizË^UPÒ5j!ÖíÒ\ÎV 
õ cd!±Ëù7\+öåÑÂþ 
x²Âúb=>T/,ÒãìògÑ ¡°î¹!¢¹Db±z¶åãØÂ~D7RX_¬çQ;úÔÃzÖÕEØ(¬;â½Õ®·mwÞ¹ $övº+ec¸Rä6¥°kèæR 
ãy´>ßØNéª#ñuEGY'f C~Æ:'!$vDZÞ-É9XgLa;n¥GêåúD'´(a$ÁBUxì¨N&mqSMEcÄÎe¹ãÃ(¬u½á¼Ù/ÉG¢óÞè(C²,@a¥²bíam·ËI¯³±kÄþ«ÇFamkÝ5ξ´0¤°ÈÑ|)Î3¸6fÓQ< 
(ÜHÜõFné­+ëi $v«s)¥ 
k]R8s ¦°Ð<Z¦Lc8ÑØGT 
7?ÌÈM±Àª 
ä¢ÚÝYÝ>nýË#ÂÚbWçó!sÚ¾#O¥=¶®¾W®OLLieÀ£Â­Ä%,k·e8èvÀäLMûÖ@ûY, 
kY_¨ZÉ@bL}øÏ ¥°Ð½ uBU³y42ãä7 Q¸°Ö]äe{¨Iü^·ï@HìRõÂVZcà) 

頁眉:

Remote Address:127.0.0.1:8070 
Request URL:http://localhost:8070/api/1/reports/email/download/RemediationStatusReport223937b2-e379-484e-8123-2918c5048a9f 
Request Method:GET 
Status Code:200 OK 
Response Headers 
view source 
Cache-Control:no-store 
Cache-Control:no-cache 
Content-Disposition:attachment; filename="RemediationStatusReport.pdf" 
Content-Transfer-Encoding:binary 
Content-Type:application/pdf 
Date:Thu, 28 May 2015 16:42:25 GMT 
Expires:Thu, 01 Jan 1970 00:00:00 GMT 
Pragma:no-cache 
Transfer-Encoding:chunked 
X-Content-Type-Options:nosniff 
X-Frame-Options:ALLOWALL 
X-XSS-Protection:1; mode=block 
Request Headers 
view source 
Accept:application/json, text/javascript, */*; q=0.01 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Cookie:i18next=en; SESSIONID={3f9ba2d9-4fa0-4bb0-b4c9-13d6c207bc7f}1uiivf0cekzsfrbexwmwtgji1.{fa9e70b7-dde3-4f98-bc53-4c45040d8afc} 
CSRFToken:b8ab6f85-7248-40ac-929d-aca96ad18244 
Host:localhost:8070 
Referer:http://localhost:8070/ 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36 

任何幫助表示讚賞。謝謝!

回答

1

刪除

@ResponseStatus(HttpStatus.OK) 
    @ResponseBody 

,並確保您在使用@Controller

註釋
+0

感謝一個類的方法,並沒有不幸的是工作。 –

相關問題