我无法使用spring MVC在浏览器中弹出下载窗口。我从Amazon S3获取文件并尝试在浏览器中对控制器使用REST调用,这应该提示用户下载PDF。当我点击端点时,窗口没有弹出,我在响应中得到了大量的随机字符,所以我想也许我没有正确使用输入/输出流。我究竟做错了什么?我还应该提到我们在前端使用Backbone JS,我知道它使用jQuerys ajax来进行REST调用..这会有什么影响吗?
控制器:
@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.åX@ASE!à
FC¼¢"r,"&A7¢ á'7b4A5Ê"7xSÈ" 7˲°L=æxÝõê~o_òý{º^uÏôt¿útWù|:öÛÐé½>5øæË´rCÕß}üò|«Oîj>jÛÍ÷ÀMû¶~ا¨Û¿&ñþÇe«ÆëqS©Ó3ò$O5Õ¥SuN:RÜozûÙ¤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Ì C@Ƕõ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[Ù[z@aEíì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¡°è>
Ñ\íá°!@O<Õvì`§¶P_B®ixÑómØZ^QØBéÂ3ý½º¹6DCtçðÒD³¡W$o)lö¶C\ÓèRÑÁºbÍ6|þµòÂèmõgÂ^«î'¶Xtã:긧¾ºÙÙ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áxt@O¾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¿õR@abútÉ`Å»T`æç|VMè@PHÈpÃÞµÀ+*`×ÄÂ"<ÊÛFç4!;ÅÞf^¾.Í!ÝÕ¾!Å5¥û{êC¦SÐÎN:JA¼O'ÓÊrhPØ,VZ")æÑöºùÏðöáäK@ü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±Ê¾@[Σ6@?óù®¾Ê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æ;r@´Îè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~ø¡ð(_o8@(ì
¾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äItÀèÔ°&wÁÄ$Ö>®ç:½î#v¦+6_×
ûÓkÂ&3¿m¢°s¸aiN¸¯Gõ2iÂÍ5>BPØ<^¾VdWçüzPØqj
[Ú
/(òh@lªÈ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ß(l@½öѶó¨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
任何帮助表示赞赏。谢谢!
答案 0 :(得分:1)
删除
@ResponseStatus(HttpStatus.OK)
@ResponseBody
并确保您的方法在使用@Controller
注释的类中答案 1 :(得分:0)
将void更改为ResponseEntity,以使其有回报。
下面的是示例。我从这里得到: Return generated pdf using spring MVC
@RequestMapping(value="/getpdf", method=RequestMethod.POST)
public ResponseEntity<byte[]> getPDF(@RequestBody String json) {
// convert JSON to Employee
Employee emp = convertSomehow(json);
// generate the file
PdfUtil.showHelp(emp);
// retrieve contents of "C:/tmp/report.pdf" that were written in showHelp
byte[] contents = (...);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
// Here you have to set the actual filename of your pdf
String filename = "output.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(contents, headers, HttpStatus.OK);
return response;
}