「java中上传下载」java中上传下载一个g的文件

博主:adminadmin 2022-11-21 15:12:09 83

本篇文章给大家谈谈java中上传下载,以及java中上传下载一个g的文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java实现文件的上传和下载

用输出流 接受 一个下载地址的网络流

然后将这个输出流 保存到本地一个文件 后缀与下载地址的后缀相同··

上传的话 将某个文件流 转成字节流 上传到某个webservice方法里

-------要代码来代码

URL url=new URL("");

URLConnection uc=url.openConnection();

InputStream in=uc.getInputStream();

BufferedInputStream bis=new BufferedInputStream(in);

FileOutputStream ft=new FileOutputStream("E://1.rar");

这是下载 上传太麻烦就不给写了

「java中上传下载」java中上传下载一个g的文件

java实现文件上传下载

那就是ftp喽,首先要有个ftp服务器

提供两种思路:

1.将ftp脚本写入.bat文件中,然后程序去执行这个批处理;

2.用jdk中net包下的api;还有一个雷同的就是下个apche的一个common-net.jar的一个依赖包

JAVA action中如何 上传 下载文件??

/**

上传文件

*/

public class FileAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

try {

FileForm fileform = (FileForm) form;

//取得请求的文件集合

Hashtable hash = fileform.getMultipartRequestHandler().getFileElements();

//得到hashtable的枚举值

Enumeration enu = hash.elements();

//如果该枚举值包含有其它的文件

while(enu.hasMoreElements()) {

//得到文件

FormFile file = (FormFile) enu.nextElement();

System.out.println(file);

add(file);

}

return mapping.findForward("yes");

} catch (Exception e) {

e.printStackTrace();

}

return super.execute(mapping, form, request, response);

}

public void add(FormFile file){

try {

//取得写文件的目录

String url=servlet.getServletContext().getRealPath("upload");

File f1=new File(url);

if(!f1.exists()){//如果文件目录不存在

f1.mkdirs();//创建目录

}

String fileName=file.getFileName();

//创建一个文件输入流

InputStream is=file.getInputStream();

OutputStream out=new FileOutputStream(url+"/"+fileName);

int byteRead=0;

byte[] by=new byte[8192];

while((byteRead=is.read(by, 0, 8192))!=-1){

out.write(by, 0, byteRead);

}

out.close();

is.close();

file.destroy();

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

下载文件

*/

页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中

Action代码如下:

ArrayList list = new ArrayList();

String path=request.getRealPath("/")+"file";

String FullPath;

//System.out.println(path);

myDir=new File(path);

list.clear();

contents=myDir.listFiles();

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

FullPath=contents.getName();

list.add(FullPath);

//System.out.println(FullPath);

}

request.setAttribute("list",list);

ActionForward forward=new ActionForward("/download.jsp");

return forward;

然后进入download.jsp中,这个页面主要负责把所有文件显示,并提供下载连接,代码如下:

%@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%

head

/style

/head

body

%ArrayList list=(ArrayList)request.getAttribute("list");

for(int i=0;ilist.size();i++)

{

String a=java.net.URLEncoder.encode((String)list.get(i));

out.print("a href=./loaded.do?name="+a+""+list.get(i)+"/abr");

}

%

/body

/html

注意,下划线画中的代码的作用,就是解决问题的所在。

接下来可以直接传入到loadedaction中,也可以通过一个form,我演示的是通过一个form

Form代码如下

package org.aeolus.struts.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

public class LoadForm extends ActionForm {

/*

*Generated Methods

*/

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

接下来就是action的代码

LoadForm doc=(LoadForm)form;

String docName = new String(doc.getName().getBytes("8859_1"));

File f;

if(docName!=""){

String docFullPath=request.getRealPath("/");

f = new File(docFullPath+"file\\"+docName);

response.reset();

response.setContentType("application/x-msdownload;charset=GBK");

System.out.print(response.getContentType());

response.setCharacterEncoding("UTF-8");

docName=java.net.URLEncoder.encode(docName,"UTF-8");

response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));

BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));

byte[] buf = new byte[1024];

int len = 0;

OutputStream out = response.getOutputStream();

while((len = br.read(buf)) 0)

out.write(buf,0,len);

out.close();

response.wait();

ActionForward forward=new ActionForward("/download.jsp");

return forward; }

return null;

注意,下划线画中的代码的作用,就是解决问题的所在。说明一下:

response.setCharacterEncoding("UTF-8");

docName=java.net.URLEncoder.encode(docName,"UTF-8");

response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));

如果不这样做你将要下载的文件名是乱码。

JAVA 上传下载文件

Java代码实现文件上传

  FormFile file=manform.getFile(); 

  String newfileName = null;

  String newpathname=null;

  String fileAddre="/numUp";

  try {

   InputStream stream = file.getInputStream();// 把文件读入

    String filePath = request.getRealPath(fileAddre);//取系统当前路径

          File file1 = new File(filePath);//添加了自动创建目录的功能

       ((File) file1).mkdir();   

    newfileName = System.currentTimeMillis()

     + file.getFileName().substring(

       file.getFileName().lastIndexOf('.'));

   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   OutputStream bos = new FileOutputStream(filePath + "/"

     + newfileName);

   newpathname=filePath+"/"+newfileName;

   System.out.println(newpathname);

   // 建立一个上传文件的输出流

    System.out.println(filePath+"/"+file.getFileName());

   int bytesRead = 0;

   byte[] buffer = new byte[8192];

   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {

    bos.write(buffer, 0, bytesRead);// 将文件写入服务器

   }

   bos.close();

   stream.close();

    } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }

如何在Java程序中实现FTP的上传下载功能

以下是这三部分的java源程序: (1)显示FTP服务器上的文件void ftpList_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); //输入的FTP服务器的ip地址 String user=userEdit.getText(); //登录FTP服务器的用户名 String passWord=passwordEdit.getText(); //登录FTP服务器的用户名的口令 String path=pathEdit.getText(); //FTP服务器上的路径 try { FtpClient ftpClient=new FtpClient(); //创建FtpClient对象 ftpClient.openServer(server); //连接FTP服务器 ftpClient.login(user, password); //登录FTP服务器 if (path.length()!=0) ftpClient.cd(path); TelnetInputStream is=ftpClient.list(); int c; while ((c=is.read())!=-1) { System.out.PRint((char) c);} is.close(); ftpClient.closeServer();//退出FTP服务器 } catch (IOException ex) {;} }(2)从FTP服务器上下传一个文件 void getButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetInputStream is=ftpClient.get(filename); File file_out=new File(filename); FileOutputStream os=new FileOutputStream(file_out); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1) { os.write(bytes,0,c); } is.close(); os.close(); ftpClient.closeServer(); } catch (IOException ex) {;} }(3)向FTP服务器上上传一个文件 void putButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetOutputStream os=ftpClient.put(filename); File file_in=new File(filename); FileInputStream is=new FileInputStream(file_in); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1){ os.write(bytes,0,c);} is.close(); os.close(); ftpClient.closeServer(); } catch (IOException ex) {;} } }

JAVA中如何将文件上传到服务器以供下载

文件放到WEB的虚拟目录,再客户端使用浏览器访问,就可以下载。

通过WEB 技术,那B服务器上得支持文件上传,才可以。

一般的WEB 服务器,是支持FTP的。

java中上传下载的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中上传下载一个g的文件、java中上传下载的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。