7.3.2 文件下载&大文件的下载 -《SSM深入解析与项目实战》

项目中所有的源码都可以在此链接的仓库中找到:https://github.com/chenhaoxiang/uifuture-ssm

7.3.2 通过ResponseEntity下载文件

7.3.1小节进行演示了文件和对象的传输,接下来演示Spring MVC中的文件下载。

文件的下载是比较简单的,直接在页面中通过访问文件的超链接也可以直接下载文件。但是如果文件名为中文名,可能在某些早期的浏览器会导致乱码,从而下载失败。

Spring MVC提供了ResponseEntity类型,可以很方便的定义返回头和返回文件名以及状态码等信息。

在FileController中增加下载方法。接来下直接看Controller类提供的下载文件方法,如代码清单7-27所示。

Controller层的下载方法

代码清单7-27:Controller层的下载方法

/**
 * 如果出现大文件,会OOM。
 * 会出现java.lang.OutOfMemor
<div class="post-text" itemprop="text"> <p>Hello fellow stakoverflowers, </p> <p>I'm having a problem with PHP/Apache. I have an application that allows the admin to upload 100Mb files. The uploading works well but I'm having problems with the downloading.</p> <p>It works perfectly with smaller files (tested with a 50Mb file) but for some reason I can't get the 100 Mb files.</p> <p>Here's my php code</p> <pre><code>$extension = 'zip'; //for testing switch ($extension) { case "dwg": $contentType="image/vnd.dwg"; break; case "dxf": $contentType="image/vnd.dxf"; break; case "pdf": $contentType="application/pdf"; break; case "zip": $contentType="application/zip"; break; case "png": $contentType="image/png"; break; case "jpeg": $contentType="image/jpeg"; break; case "jpg": $contentType="image/jpg"; break; case "gif": $contentType="image/gif"; break; default: $contentType = ''; } @header("Content-type: " . $contentType); @header("Content-Disposition: attachment; filename=$filename"); @header("Cache-Control: no-cache, must-revalidate"); @header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // passed date echo file_get_contents($url); </code></pre> <p>I've also tried other solutions I found on SO.</p> <pre><code>header('Content-Description: File Transfer'); header('Content-Transfer-Encoding: binary'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-type: " . $contentType); header("Content-Disposition: attachment; filename=\"".$filename."\""); header("Content-Length: ".filesize($url)); echo self::url_get_contents(URL_PUBLIC . $url); ... private function url_get_contents ($url) { if (!function_exists('curl_init')){ die('CURL is not installed!'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); echo $output; return $output; } </code></pre> <p>Or </p> <pre><code>$file_contents = fopen($url, "r"); echo $file_contents; fclose($file_contents); </code></pre> <p>All solutions return the same thing. A file size of 0 Kb. Like I said, smaller sized files works.</p> <p>Also, when I test locally, the 100 Mb size files download correctly so my guess is that the problem comes from the server. I've changed the php.ini as follows </p> <pre><code>register_globals = Off magic_quotes_gpc = Off post_max_size = 128M memory_limit = 256M upload_max_filesize = 128M max_execution_time = 120 expose_php = off session.save_path = /tmp mysqli.default_socket = /tmp/mysql5.sock mysql.default_socket = /tmp/mysql5.sock </code></pre> <p>It's probably a memory limit problem but not sure.</p> </div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值