『壹』 java程序 如何设置下载文件的保存路径
生成一个文件选择窗口,让用户选择路径
『贰』 java 下载服务器端文件,路径怎么写
//得到当前路径下的upload文件夹String strPath = request.getSession().getServletContext().getRealPath("/upload");
『叁』 java里http服务器如何将文件制成一个下载路径
/***文件下载*/@RequestMapping("/downloadfile")(StringresStr,HttpServletResponseresponse){PrintWriterpw=null;StringBuffersb=newStringBuffer();try{StringfileName="文件名";response.setCharacterEncoding("UTF-8");response.setContentType("application/x-msdownload");response.addHeader("Cache-Control","no-cache,no-store,must-revalidate");response.addHeader("charset","utf-8");response.addHeader("Pragma","no-cache");response.setHeader("Content-Disposition","attachment;filename=""+fileName+"";filename*=utf-8''"+fileName);sb.append(resStr);pw=response.getWriter();pw.write(sb.toString());pw.close();response.flushBuffer();}catch(IOExceptione){logger.info("下载文件出错");e.printStackTrace();if(pw!=null){pw.close();}}finally{if(pw!=null){pw.close();}}}
resStr 字段传写入文件里的内容
『肆』 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();}
『伍』 Java文件下载怎么实现的
下载就很简单了把你要下载的文件做成超级链接,可以不用任何组件比如说下载一个word文档<a href="名称.doc">名称.doc</a>路径你自己写import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.ProtocolException;import java.net.URI;import java.net.URL;import java.util.Random;/** * * 实现了下载的功能*/ public class SimpleTh {public static void main(String[] args){ // TODO Auto-generated method stub //String path = "http://www.7cd.cn/QingTengPics/倩女幽魂.mp3";//MP3下载的地址 String path ="http://img.99luna.com/music/%CF%EB%C4%E3%BE%CD%D0%B4%D0%C5.mp3"; try { new SimpleTh().download(path, 3); //对象调用下载的方法 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String getFilename(String path){//获得文件的名字 return path.substring(path.lastIndexOf('/')+1); }public void download(String path,int threadsize) throws Exception//下载的方法 {//参数 下载地址,线程数量 URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection();//获取HttpURLConnection对象 conn.setRequestMethod("GET");//设置请求格式,这里是GET格式 conn.setReadTimeout(5*1000);// int filelength = conn.getContentLength();//获取要下载文件的长度 String filename = getFilename(path); File saveFile = new File(filename); RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.setLength(filelength); accessFile.close(); int block = filelength%threadsize ==0?filelength/threadsize:filelength/threadsize+1; for(int threadid = 0;threadid<=threadsize;threadid++){ new DownloadThread(url,saveFile,block,threadid).start(); } } private final class DownloadThread extends Thread{ private URL url; private File saveFile; private int block;//每条线程下载的长度 private int threadid;//线程id public DownloadThread(URL url,File saveFile,int block,int threadid){ this.url = url; this.saveFile= saveFile; this.block = block; this.threadid = threadid; } @Override public void run() { //计算开始位置的公式:线程id*每条线程下载的数据长度=? //计算结束位置的公式:(线程id+1)*每条线程下载数据长度-1=? int startposition = threadid*block; int endposition = (threadid+1)*block-1; try { try { RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd"); accessFile.seek(startposition);//设置从什么位置写入数据 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5*1000); conn.setRequestProperty("Range","bytes= "+startposition+"-"+endposition); InputStream inStream = conn.getInputStream(); byte[]buffer = new byte[1024]; int len = 0; while((len = inStream.read(buffer))!=-1){ accessFile.write(buffer, 0, len); } inStream.close(); accessFile.close(); System.out.println("线程id:"+threadid+"下载完成"); } catch (FileNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }}
『陆』 下载的Java路径找不到,怎么办
出现这种问题的原因是因为路径有中文,建议默认安装不要修改jdk的默认路径,之后配置环境变量即可。如:jdk安装在“D:\Program Files\jdk1.7.0_10”第一步:新建“java_home”值,输入“D:\Program Files\jdk1.7.0_10”;第二步:新建“classpath”值,输入“.;%java_home%\lib”;第三步:在path中增加“%java_home%\bin”;备注:配置环境变量在“计算机”右击“属性”,之后选择“高级环境变量”,在选择“环境变量”即可。
『柒』 java实现下载文件时让用户自定义选择下载路径
这个下载路径不是由java控制的,而是你的浏览器控制的,把浏览器的默认下载路径去掉就可以了
『捌』 java中如何拿到用户设置的下载路径
<input id="DownLoad" name="DownLoad" type="file" value="下载">把type改成file,不要使用download
『玖』 用java下载指定路径下的文件夹,下载内容包含指定文件夹及其包含的文件夹子文件!!
这个做不了的, 在计算机,你用命令去复制粘贴都需要指定是否递归复制也就是说内,如果你想下载指定的容文件夹,你需要做很多的处理,一个一个文件的下载,然后下载到相对路径中去,还有一种方案就是直接将文件夹打包再下载
『拾』 用java下载指定路径下的文件夹,下载内容包含指定文件夹及其包含的文件夹子文件!!!
public static void main(String[] args) throws InterruptedException { // 指定文件夹File file = new File("D:\\downloads\\");List<File> fileList = null;// 包含字符String filter = "j";if (file != null) {if (file.isDirectory()) {File[] fileArray = file.listFiles();if (fileArray != null && fileArray.length > 0) {fileList = new ArrayList<File>(); // 包括文件,文件夹的判断for (File f : fileArray) {String fileName = f.getName();if (fileName.indexOf(filter) != -1) {fileList.add(f);}}}} else {System.out.println("Not Directory.");}}if (fileList != null && fileList.size() > 0) {for (File f : fileList) {System.out.println(f.getName());}}}希望对你有所帮助。。。
未经允许不得转载:山九号 » 下载路径文件java|Java 下载文件的方法怎么写