文件下载怎么写|Java 下载文件的方法怎么写



① 用jsp怎么编写文件下载代码

下面是我写的一个小例子,下载远程文件urlString,到本地文件localFile.成功返回True,不成功返回False.把这代码插入到你JSP中用到的地方就OK了:) public boolean downLoadFile(String urlString, String localFile) { URL url; byte[] buffer = new byte[512]; int size = 0; boolean success = false; try { url = new URL(urlString); BufferedInputStream stream = new BufferedInputStream(url.openStream()); FileOutputStream fos = new FileOutputStream(localFile); while ((size = stream.read(buffer)) != -1) { fos.write(buffer, 0, size); } fos.close(); stream.close(); success = true; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return success; }

② 用linux命令 在ftp上下载多个文件,该怎么写呢谢谢!

这个是挺容易的。你可以使用help来查询可用命令。标准的情况下,mget可以接受通配符(即像shell一样使用通配符表示一类文件),这样就可以一次下载多个文件了。更高级的一些FTP客户端还可以支持下载列表等功能。你也可以使用axel或wget来使用列表以进行多个文件下载(在这种情况下应该是写成这样:ftp://username:[email protected]/filename,具体能不能用小弟还没试过,你可以查询相关资料或者找o'reilly的书籍看看)。

③ php 实现url的文件下载,不是fopen的那种要怎么写

file_get_contents

④ ASP如何写文件下载代码

直接发送下载的代码,Set objStream = Server.CreateObject("ADODB.Stream")objStream.OpenobjStream.Type = 1objStream.LoadFromFile filenameSelect Case lcase(Right(filename, 4))Case ".asf" ContentType = "video/x-ms-asf"Case ".avi" ContentType = "video/avi"Case ".doc" ContentType = "application/msword"Case ".xls" ContentType = "application/vnd.ms-excel"Case ".gif" ContentType = "image/gif"Case ".jpg", "jpeg" ContentType = "image/jpeg"Case ".wav" ContentType = "audio/wav"Case ".mp3" ContentType = "audio/mpeg3"Case ".mpg", "mpeg" ContentType = "video/mpeg"Case ".rtf" ContentType = "application/rtf"Case ".htm", "html" ContentType = "text/html"Case ".txt" ContentType = "text/plain"Case Else ContentType = "application/octet-stream"End SelectResponse.AddHeader "Content-Disposition", "attachment; filename=" & shortNameResponse.AddHeader "Content-Length", fileSize Response.Charset = "UTF-8"Response.ContentType = ContentType Response.BinaryWrite objStream.ReadResponse.Flushresponse.Clear()objStream.CloseSet objStream = Nothing

⑤ JSP文件下载代码怎么写

通过设置 page 的 contentType 可以实现下载功能 。如 : <%@ page contentType="application/msword" pageEncoding="UTF-8"%>那么打相此 jsp 页面会弹出保存 *.doc 文档的对话框专 。这里列出了更多的属设置类型 :http://blog.csdn.net/oldcrane/article/details/3850244

⑥ java中文件下载该怎么写代码求高手指导

当流输出给浏览器

⑦ java 想做一个文件下载 不知道怎么写

如果是本机到本机就用文件输入输出流,如果是从别的机子下载,就用socket先建立连接再获取到流来传送文件。

⑧ asp.net MVC 中文件下载的代码怎么写,不要求上传

控制器中写一个Action,有直接返回File()类型的,该方法其实就是下载

publicActionResultExportFile(){Services.ImportAndExportmanage=newServices.ImportAndExport();stringfileName="abc.xls";//文件专名stringfilePath="D:\abc.xls";//文件路径stringMIME="application/vnd.ms-excel";//文件类型属returnFile(filePath,MIME,fileName);}

⑨ Java 下载文件的方法怎么写

参考下面public HttpServletResponse download(String path, HttpServletResponse response) {try {// path是指欲下载的文件的路径。File file = new File(path);// 取得文件名。String filename = file.getName();// 取得文件的后缀名。String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();// 以流的形式下载文件。InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的Headerresponse.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));response.addHeader("Content-Length", "" + file.length());OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");toClient.write(buffer);toClient.flush();toClient.close();} catch (IOException ex) {ex.printStackTrace();}return response;}// 下载本地文件public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {String fileName = "Operator.doc".toString(); // 文件的默认保存名// 读到流中InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径// 设置输出的格式response.reset();response.setContentType("bin");response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");// 循环取出流中的数据byte[] b = new byte[100];int len;try {while ((len = inStream.read(b)) > 0)response.getOutputStream().write(b, 0, len);inStream.close();} catch (IOException e) {e.printStackTrace();}}// 下载网络文件public void downloadNet(HttpServletResponse response) throws MalformedURLException {int bytesum = 0;int byteread = 0;URL url = new URL("windine.blogdriver.com/logo.gif");try {URLConnection conn = url.openConnection();InputStream inStream = conn.getInputStream();FileOutputStream fs = new FileOutputStream("c:/abc.gif");byte[] buffer = new byte[1204];int length;while ((byteread = inStream.read(buffer)) != -1) {bytesum += byteread;System.out.println(bytesum);fs.write(buffer, 0, byteread);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}//支持在线打开文件的一种方式public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {File f = new File(filePath);if (!f.exists()) {response.sendError(404, "File not found!");return;}BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));byte[] buf = new byte[1024];int len = 0;response.reset(); // 非常重要if (isOnLine) { // 在线打开方式URL u = new URL("file:///" + filePath);response.setContentType(u.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename=" + f.getName());// 文件名应该编码成UTF-8} else { // 纯下载方式response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());}OutputStream out = response.getOutputStream();while ((len = br.read(buf)) > 0)out.write(buf, 0, len);br.close();out.close();}


赞 (0)