NutzCN Logo
问答 使用nutz实现文件下载功能时出错
发布于 2459天前 作者 qq_34441c67 3159 次浏览 复制 上一个帖子 下一个帖子
标签:

我们现在做的项目需要文件下载功能,我打算采用nutz内置的文件下载方式,是在nutz社区里面看到的,但是在实际操作中,下载时响应的数据却是乱码,不知道怎么回事?我做的是需要下载各种格式的数据,主要是公文附件,
求大神指教!!本人菜鸟一枚

@Ok("raw")
	@At
	public  File downloadFile(@Param("fileUrl")String fileUrl) throws IOException {
		//全路径
		if(fileUrl != null){
			File file = new File(fileUrl);
			if(file.exists() && file.isFile()){
				return file;
			}
			return null;
		}else{
			return null;
		}
	}

这是页面中返回的数据:[一个txt文件]
�����ĩ�ã�������
��Զ��������ǰ��������ѵ������ҹ�˾����ѵ�ƻ����˵���������֪����
������ѵ�γ̣�
5��22�� ��һ ����˼���� ������ѵ��C1���ڼ��ɲ�� 17:40-19:40 ��ʦ���𾧾� ��Ԫ��
5��23�� �ܶ� �������� ʵʩ��ѵ�� ͼ�����š����𼯳����� 17:40-19:40 ��ʦ��������ʦ ����
����׼ʱ�μӣ�
�γ����ӣ�
��һ������˼C1�γ�
�������⣺ ��������Ʒ��ѵ--����˼C1�γ�--����
����ʱ�䣺 2017��05��22�գ�����һ 17:40 (GMT+8:00) �������Ϻ�
�� PC�� Mac�� iPhone��iPad����׿�ֻ���ƽ���ϣ�����������Ӽ������ɼӻ� �� http://www.hhmeeting.cn/j/1349917046?pwd=doN4KejYQ0M%3D
�������룺seeyon

ͨ��H.323/SIP �豸�ӻ᣺
���� 221.228.231.45
����ţ� 134 991 7046
�������룺 584383

13 回复

试试

@Ok("raw:stream")

还是一样的,返回的是乱码,

看看浏览器得到的header信息

这是header信息
Request URL:http://localhost:8080/school/file/downloadFile
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Content-Disposition:attachment; filename="%E7%A4%BA%E4%BE%8B%E5%9B%BE%E7%89%87_02.jpg"
Content-Length:89100
Content-Type:application/octet-stream;charset=UTF-8
Date:Thu, 27 Jul 2017 02:11:25 GMT
Server:Apache-Coyote/1.1
X-Powered-By:nutz/1.r.61.r2 2017-04-18 <nutzam.com>
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:84
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:JSESSIONID=7A4856DD1F8A2C3F41FE3EF678E9B3C4
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/school/templete/20170523/main.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
{"fileUrl":"D:\apache-tomcat-7.0.53\webapps\school\upload\示例图片_02.jpg"}:

X-Requested-With:XMLHttpRequest

难道你用的是ajax???

恩,是的,是通过系统封装好的ajax访问的,

ajax可不能下载文件, 开个iframe或者直接操作dom才可能

恩,好的我去试试,

非常感谢@wendal
现在已经做好了,可以正常下载文件了。
@Ok("raw")
@At
public File downloadFile(@Param("fileid")Long fileid) throws IOException {
//全路径
SysUploadfiles sysUploadfiles = super.dao.fetch(SysUploadfiles.class,Cnd.where("FileId","=",fileid));
String fileUrl = sysUploadfiles.getFileurl();
if(fileUrl != null){
File file = new File(fileUrl);
if(file.exists() && file.isFile()){
return new File(fileUrl);
}
return null;
}else{
return null;
}
}
页面中使用window.open方法解决的。
function downloadFile(a){
var fileid = $(a).attr("fileid");
window.open("../../file/downloadFile" + "?fileid=" + fileid,"159")
}

好(✪▽✪)

@wendal 请问如何下载服务器上的某个 “文件夹”?使用上面的方法只能下载具体的文件。谢谢!

文件夹只能做成zip等压缩包,实时或提前生成压缩包

恩,不能直接下载文件夹。好的,我就先生成压缩包吧,谢谢O(∩_∩)O~

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