资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java下载功能代码 java文件下载代码

帮我看看这段java下载代码,文件名为中文时,报错,无法找到指定文件

代码一共6句,前4句没问题,运行下载是ok的,

开平网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联公司于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司

最后一句有点不明白,为什么用BufferedInputStream?这个是读文件的,不知道你后面是如何写的,难道还要把文件读到内存?在向客户端发送出去??这样的话下载GB级文件内存溢出。

BufferedInputStream和BufferedOutputStream 用了之后,一定要flush(),这样也许会解决你的中文下载报错。

我直接用BufferedOutputStream 下载文件成功,前面是用你的,后面如下:

InputStream in = new FileInputStream(file);// 将文件装换成缓冲流

OutputStream out = response.getOutputStream(); // 获取response中得下载对象

BufferedOutputStream bufo = new BufferedOutputStream(out); // 对象转换成字符流

int length = 0; // 读取本地文时,记录本次文件读取内容大小

byte[] buffer = new byte[524288]; // 每次推送 512KB

while ((length = in.read(buffer)) != -1) // 读取本地文件,并在存放在buffer 数组

{

bufo.write(buffer, 0, length);// 预备向客户端推送

bufo.flush();// 清空缓存,并立即推送

}

in.close();

out.close();

bufo.close();

不建议写 response.setContentLength(int); 大并发时,这个容易出问题。

javaweb下载的代码怎么写

这个是用spSmartUpload实现的 使用的时候导入smartupload的jar包

jsp页面

body

%

File f = new File("e:\\abc\\");

String[] fnames = f.list();

for(int i = 0; i fnames.length; i++){

%

a href="down?fname=%=fnames[i]%"%=fnames[i]%/abr

%}%

/body

servlet中

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String fname = new String(request.getParameter("fname").getBytes(

"iso-8859-1"));

SmartUpload su = new SmartUpload();

su.initialize(getServletConfig(), request, response);// 初始化

try {

su.setContentDisposition(null);//出现下载提示

su.downloadFile("e:\\abc\\" + fname);

} catch (SmartUploadException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

java中怎么实现下载功能,最好能有代码

private File uploadify;

private String uploadifyFileName;

public String uploadFile1() throws Exception {

String extName = "";// 扩展名

String newFileName = "";// 新文件名

String nowTime = df.format(new Date());// 当前时间

String random = "-" + (Math.round(Math.random() * 9000) + 1000);// 随机函数

String path = "uploads/" + nowTime.substring(0, 6) + "/"

+ nowTime.substring(0, 8) + "/";// 保存路径

String savePath = ServletActionContext.getServletContext().getRealPath(

"");

savePath = savePath.replace("\\", "/");

if (!savePath.substring(savePath.length()).equals("/"))

savePath = savePath + "/";

savePath = savePath + path;

// 获取扩展名

if (uploadifyFileName.lastIndexOf(".") = 0) {

extName = uploadifyFileName.substring(uploadifyFileName

.lastIndexOf("."));

}

newFileName = uploadifyFileName.substring(0,

uploadifyFileName.lastIndexOf("."))

+ nowTime.substring(8) + random + extName;

File file = new File(savePath);

if (!file.exists())

file.mkdirs();

uploadify.renameTo(new File(savePath + newFileName));

/*

* HttpServletResponse response = ServletActionContext.getResponse();

* response.setCharacterEncoding("utf-8");

* response.getWriter().print(uploadifyFileName+"上传成功");

*/

String ctx = Struts2Utils.getRequest().getContextPath();

Struts2Utils.renderText(ctx + "/" + path + newFileName);

Wenjdetail detail = new Wenjdetail();

String pt = path + newFileName;

detail.setName(uploadifyFileName);

detail.setUrl(pt);

wenjdetailManager.saveWenjdetail(detail);

HttpServletResponse response = ServletActionContext.getResponse();

response.setCharacterEncoding("utf-8");

response.getWriter().print("," + detail.getId());

return null; // 这里不需要页面转向,所以返回空就可以了

}

java 实现下载

首先有2种方法:

1. 用超链接标签直接链接至文件路径,前提是这个文件在项目系统的相对路径下;

2. 利用java代码实现,先读取文件,然后以流的形式发送到浏览器;这种方法就是不管文件在操作系统的什么地方都可以读取;而且还可以重写客户接受的文件的名称。

类似的代码你上网查查就可以了,很多的


当前标题:java下载功能代码 java文件下载代码
文章分享:http://cdkjz.cn/article/dodhsih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220