nutz是怎么实现下载的呀,我只在mvc看到了上传,没有下载呀?
@wendal 看不懂啊, 将服务器的文件下载到客户端, 有木有长点的代码可以参考呀,
@At
@Ok("raw")
public File download(@Param("name")String name) {
return new File("xxx/xxx/xx/x/" + name);
}
@qq_88adaffc 文件下载只能跳过去,不能js操作的
来自炫酷的 NutzCN
@wendal 我在js里用ajax把name传过去,但是只是在控制台显示:
2015-12-17 08:38:33,374 [http-bio-8080-exec-5] DEBUG [org.nutz.mvc.impl.UrlMappingImpl] - Found mapping for [GET] path=/sava : Work.download(...)
2015-12-17 08:38:33,374 [http-bio-8080-exec-5] DEBUG [org.nutz.ioc.impl.NutIoc] - Get 'work'<class address.web.Work>
2015-12-17 08:38:33,389 [http-bio-8080-exec-5] DEBUG [org.nutz.mvc.view.RawView] - File downloading ... D:\demo.doc
别的什么都没反应呀?
@Rekoe 我在js里把name传过去,但是只是在控制台显示:
2015-12-17 08:38:33,374 [http-bio-8080-exec-5] DEBUG [org.nutz.mvc.impl.UrlMappingImpl] - Found mapping for [GET] path=/sava : Work.download(...)
2015-12-17 08:38:33,374 [http-bio-8080-exec-5] DEBUG [org.nutz.ioc.impl.NutIoc] - Get 'work'<class address.web.Work>
2015-12-17 08:38:33,389 [http-bio-8080-exec-5] DEBUG [org.nutz.mvc.view.RawView] - File downloading ... D:\demo.doc
别的什么都没反应呀?
如果可以 的话 你这样试试
<script type="text/javascript">
function down()
{
$.post("${base}/onlineDown", function(data){
});
}
</script>
@Rekoe 不能呀,我是这么写的:
@At("sava")
@Ok("raw")
public File download(@Param("name")String name) {
return new File("D:\" + name);
}
访问他就只是控制台输出
我本地的测试
http://106.2.185.120:8080/down
code
@At
@Ok("raw")
public File down() {
return Files.findFile("D:/hero.xml");
}
@Rekoe 点你的地址果然下载有下载提示,我再试试
@Rekoe 为什么我的Files里没有findFile这个方法呀?
@Rekoe 是不是还需要配置其他的呀,为什么我的点了链接,只是控制台上除了两行代码:
2015-12-17 11:18:41,607 [http-bio-8080-exec-24] DEBUG [org.nutz.mvc.impl.UrlMappingImpl] - Found mapping for [GET] path=/sava : Work.download(...)
2015-12-17 11:18:41,607 [http-bio-8080-exec-24] DEBUG [org.nutz.ioc.impl.NutIoc] - Get 'work'<class address.web.Work>
根本就不提醒下载呀???
直接访问url,还不行再查
来自炫酷的 NutzCN
@wendal
你好,我在使用这个实现文件下载的时候 下载的文件是被存放到了服务器端,这怎么解释呢?
并没有实现文件的客户端下载,其他人访问我的服务器的时候,也只是生成了一个文件保存在我本地而已。
@At
// @Ok("redirect:/")
@Ok("raw")
@Fail("error")
@Log(isEnabled=false)
@AuthBy(check=false)
public synchronized File getInspectionReportList(HttpServletRequest request, Ioc ioc, HttpServletResponse response){
File result=null;
InspectionReportService inspectionReportService = ioc.get(InspectionReportService.class, "inspectionReportService");
InspectionReportUtil inspectionReportUtil = ioc.get(InspectionReportUtil.class,"inspectionReportUtil");
String ss = inspectionReportUtil.createReport(inspectionReportService.getMonitorInfoDetail(request));
File doc = inspectionReportUtil.createDoc(ss, request);
String fileName=doc.getName();
String tem= System.getProperty("java.io.tmpdir");
result=new File(tem+File.separator+fileName+".doc");
try {
FileUtils.copyFile(doc, result);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@xzfxz 除非你是用ajax来访问这个地址
@wendal
哥们 我解决了,代码如下。说白了,就是我自己用jsp技术实现了下载
@At
// @Ok("redirect:/")
@Ok("raw")
@Fail("error")
@Log(isEnabled=false)
@AuthBy(check=false)
public synchronized File getInspectionReportList(HttpServletRequest request, Ioc ioc, HttpServletResponse response){
String flag=null;
File result=null;
InspectionReportService inspectionReportService = ioc.get(InspectionReportService.class, "inspectionReportService");
InspectionReportUtil inspectionReportUtil = ioc.get(InspectionReportUtil.class,"inspectionReportUtil");
String ss = inspectionReportUtil.createReport(inspectionReportService.getMonitorInfoDetail(request));
File doc = inspectionReportUtil.createDoc(ss, request);
//return doc;
String fileName=doc.getName();
try {
fileName = URLEncoder.encode(fileName, Encoding.UTF8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletContext servletContext = request.getServletContext();
if(null!=doc){
try {
InputStream in = new FileInputStream(doc);
servletContext = request.getServletContext();
String mimeType = servletContext.getMimeType(doc.getName());
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
ServletOutputStream outputStream = response.getOutputStream();
byte[] b= new byte[1024];
int i=0;
while ((i=in.read(b))!=-1){
outputStream.write(b,0,i);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return result ;
}
@xzfxz 不深究一下?