NutzCN Logo
问答 nutz是怎么实现下载的
发布于 3046天前 作者 zozoh44 5580 次浏览 复制 上一个帖子 下一个帖子
标签: mvc

nutz是怎么实现下载的呀,我只在mvc看到了上传,没有下载呀?

29 回复
@Ok("raw")

return file;

@wendal 看不懂啊, 将服务器的文件下载到客户端, 有木有长点的代码可以参考呀,

@At
@Ok("raw")
public File download(@Param("name")String name) {
    return new File("xxx/xxx/xx/x/" + name);
}

@wendal
我是个小白,再弱弱的问下,
这个文件是返回到js吗?怎么保存到本地呀?

@qq_88adaffc 文件下载只能跳过去,不能js操作的

来自炫酷的 NutzCN

@qq_88adaffc
你通过js跳转到你的下载地址就可以了

@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

别的什么都没反应呀?

@qq_88adaffc
首先 你直接访问地址能否下载文件?

如果可以 的话 你这样试试

<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);
}

访问他就只是控制台输出

@qq_88adaffc
你D盘有个文件吗?

我本地的测试

http://106.2.185.120:8080/down

code

	@At
	@Ok("raw")
	public File down() {
		return Files.findFile("D:/hero.xml");
	}

@Rekoe 点你的地址果然下载有下载提示,我再试试

@Rekoe 为什么我的Files里没有findFile这个方法呀?

@qq_88adaffc 导错包了。。

@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 嗯嗯,直接访问地址可以了,

第一次接触者下载文件,有点不知所措,

现在突然发现这么简单呀

@qq_88adaffc 下载一个文件和打开一个网页,对服务器来说没啥区别

@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 ;
}

像我这种,想要多点几个订单号,只能依靠jq,本地测试不能弹出下载,文件倒是我知道在哪里。

@At({"/exportExcel","/exportExcel/?"})
	@Ok("raw")
	@RequiresAuthentication
	public Object getListToExcel(String no,@Param("nos") String[] orderNos,HttpServletRequest request) {

返回一个raw,在windows下出现了下载文件,现在在mac不提示,都是chrome浏览器

搞错文件了,失误

添加回复
请先登陆
回到顶部