【发布时间】:2016-04-09 19:55:07
【问题描述】:
基本上,我的系统会生成 url 以从我的服务器下载文件。我使用 Struts 进行此操作,结果为“流”类型。这工作很棒。 现在我想注意每个下载的文件是否已完全下载。
我一直在寻找一段时间,但真的不知道如何继续。我是这方面的初学者。
**
新动作:
**
public String download(){
try {
String path = Init.getWebInfPath() + UploadManager.getInstance().getProperty("UPLOAD_DIR") + content.getZipFile();
file = new FileInputStream(new File(path));
} catch (Exception e) {
return ERROR;
}
Long overallSize = null;
try {
overallSize = file.getChannel().size();
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=\""+content.getZipFile()+"\"");
response.setHeader("Content-Length", String.valueOf(overallSize));
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
int read;
read = file.read();
while (read>0){
sos.write(read);
read = file.read();
}
} catch(Exception e){
return ERROR;
}
return NONE;
}
**
新的 xml:
**
<struts>
<package name="content" namespace="/content" extends="default">
<action name="download" class="com.tictapps.adserver.web.content.ContentAction" method="download">
<result name="none"/>
</action>
</package>
</struts>
【问题讨论】:
-
代码看起来不错。
标签: java apache http tomcat struts2