导航:首页 > 文件教程 > java传输文件夹

java传输文件夹

发布时间:2021-10-25 06:19:44

A. java怎么传送文件夹下面所有的文件有没有什么可行的程序

File 下面有个listFile方法可以遍历一个文件夹下的所有文件,你可以去看看那

B. java中如何将上传的图片复制到指定文件夹中。

public static void File(File sourceFile, File targetFile) throws IOException {
BufferedInputStream inBuff=null;
BufferedOutputStream outBuff=null;
try {
// 新建文件输入流并对它进行缓冲
inBuff=new BufferedInputStream(new FileInputStream(sourceFile));

// 新建文件输出流并对它进行缓冲
outBuff=new BufferedOutputStream(new FileOutputStream(targetFile));

// 缓冲数组
byte[] b=new byte[1024 * 5];
int len;
while((len=inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if(inBuff != null)
inBuff.close();
if(outBuff != null)
outBuff.close();
}
}

C. java文件上传文件路径

String newFilePath = "new Path" + "\\" + newfile.getFileName;

File file = new File(String newFilePath);

D. JAVA实现文件夹传输

//这是一个传输的程序.下面是两个程序,一个名为MyClient,
//是客户端.另一个名为MyServer,为服务器端.这两个程序实现的
//是将客户端的e盘名为"1"的文件夹里的所有内容传输到服务器端
//的e盘名为"2"的文件夹里.运行时,一定要先运行MyServer,否则会
//出错.这个程序我用两台电脑在局域网里运行过多次,一切正常,速度
//在600KB/s左右.如果运行MyServer后,再运行MyClient时,出现错误
//建议再运行一下.因为它们可能在建立连接时由于网络连接不好而
//未能连接成功而出错.程序是肯定没有问题的.
import java.io.*;
import java.net.*;
public class MyClient
{
Socket client;
boolean flag=true;
FileInputStream fis;//此输入流负责读取本机上要传输的文件
DataOutputStream dos;//此输出流负责向另一台电脑(服务器端)传输数据
DataInputStream dis;//此输入流负责读取另一台电脑的回应信息
public static void main(String[] args)
{
new MyClient().ClientStart();
}
public void ClientStart()
{
try
{
client=new Socket("192.168.2.101",30000);//服务器端的IP,(这个只是在局域网内的)我的是这个,你的根据实际而定
//client=new Socket("localhost",30000);
System.out.println("已连接");
dos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
transmit(new File("e:\\1\\"));
String s="/]00";//提示传输完毕的标记
byte b[]=s.getBytes();
dos.write(b,0,s.length());
dos.flush();
}
catch(IOException e)
{
System.out.println("Error");
}
}
public void transmit(File f)throws IOException//这是传输的核心,而且将被递归
{
byte b[];
String ts;
int ti;
for(File f1:f.listFiles())
{ //首先通过if语句判断f1是文件还是文件夹
if(f1.isDirectory()) //fi是文件夹,则向服务器端传送一条信息
{
ts="/]0f"+(f1.getPath().replace("e:\\1\\",""));//"/]0f"用于表示这条信息的内容是文件夹名称
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
transmit(f1);//由于f1是文件夹(即目录),所以它里面很有可能还有文件或者文件夹,所以进行递归
}
else
{
fis=new FileInputStream(f1);
ts="/]0c"+(f1.getPath().replace("e:\\1\\",""));//同上,表示这是一个文件的名称
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
dos.writeInt(fis.available());//传输一个整型值,指明将要传输的文件的大小
dos.flush();
dis.read();
b=new byte[10000];
while(fis.available()>0)//开始传送文件
{
ti=fis.read(b);
dos.write(b,0,ti);
dos.flush();
}
dos.flush();
fis.close();
dis.read();
}
}

}
}

import java.io.*;
import java.net.*;
public class MyServer
{
ServerSocket server=null;
Socket client=null;
boolean flag=true;
DataInputStream dis;
DataOutputStream dos;
FileOutputStream fos;
public static void main(String[] args)
{
new MyServer().ServerStart();
}
public void ServerStart()
{
try
{
server=new ServerSocket(30000);
System.out.println("端口号:"+server.getLocalPort());
client=server.accept();
System.out.println("连接完毕");
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
String answer="g";
byte ans[]=answer.getBytes();
byte b[]=new byte[1000];
int ti;
new File("e:\\2").mkdirs();
while(flag)
{
ti=dis.read(b);
dos.write(ans);
String select=new String(b,0,ti);
if(select.contains("/]0f"))
{
File f=new File("e:\\2\\"+(select.replace("/]0f","")));
System.out.println("creat directory");
f.mkdirs();
}
else if(select.contains("/]0c"))
{
fos=new FileOutputStream("e:\\2\\"+(select.replace("/]0c","")));
String cs;
boolean cflag=true;
int tip=dis.readInt();
dos.write(ans);
while(tip>0)
{
ti=dis.read(b,0,(tip>1000?1000:tip));
tip=tip-ti;
cs=new String(b,0,4);
fos.write(b,0,ti);
}
fos.flush();
fos.close();
dos.write(ans);
}
else if(select.contains("/]00"))
{
flag=false;
}
}
dis.close();
client.close();
server.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
}

E. JAVA 传输文件

//以前写的一个文件传输的小程序,有客户端和服务器端两部分,服务器可//以一直运行,客户端传输完一个后退出,当然你也可以根据你的需要改。
//服务器端可以支持多个客户端同时上传,用到了多线程
/**
* 文件传输,客户端
* @aurth anyx
*/
//package per.anyx.ftp;

import java.net.*;
import java.io.*;

public class FtpClient{
public static void main(String[] args){
if(args.length != 3){
System.out.println("Usage: FtpClient host_add host_port src_file");
System.exit(0);
}
File file = new File(args[2]);
if(!file.exists() || !file.isFile()){
System.out.println("File \"" + args[2] + "\" does not exist or is not a normal file.");
System.exit(0);
}
Socket s = null;
FileInputStream in = null;
OutputStream out = null;
try{
s = new Socket(args[0], Integer.parseInt(args[1]));
in = new FileInputStream(file);
out = s.getOutputStream();

byte[] buffer = new byte[1024*8];
int len = -1;
System.out.println("File tansfer statr...");
while((len=in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
System.out.println("File tansfer complete...");
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
System.exit(1);
}finally{
try{
if(in != null) in.close();
if(out != null) out.close();
if(s != null) s.close();
}catch(Exception e){}
}
}
}

/**
* 文件传输,服务器端
* @aurth anyx
*/
//package per.anyx.ftp;

import java.net.*;
import java.io.*;

public class FtpServer{
public static void main(String[] args){
if(args.length != 1){
System.out.println("Usage: FtpServer server_port");
System.exit(0);
}
ServerSocket ss = null;
try{
ss = new ServerSocket(Integer.parseInt(args[0]));
System.out.println("FtpServer start on port ..." + args[0]);
while(true){
Socket s = ss.accept();
new FtpThread(s).start();
System.out.println(s.getInetAddress().getHostAddress() + " connected.");
}
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}finally{
try{
if(ss != null) ss.close();
}catch(Exception e){}
}
}
}

class FtpThread extends Thread{
Socket s;
long fileName = 0;
public FtpThread(Socket s){
this.s = s;
}
public void run(){
FileOutputStream out = null;
InputStream in = null;
File file = null;
do{
file = new File("" + (fileName++));
}while(file.exists());
try{
out = new FileOutputStream(file);
in = s.getInputStream();

byte[] buffer = new byte[1024*8];
int len = -1;
while((len=in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}finally{
try{
if(in != null) in.close();
if(out != null) out.close();
if(s != null) s.close();
System.out.println(s.getInetAddress().getHostAddress() + " connect closed..");
}catch(Exception e){}
}
}
}

F. 怎样用java实现压缩传输文件夹

java有ZIP相关的类用来把文件夹打包成zip文件,然后传输
或者用ZIPInputStream/ZIPoutputStream压缩数据流来传输
不知道你要的是哪种

G. java怎么把文件传输到file

common-fileupload是jakarta项目组开发的一个功能很强大的上传文件组件
下面先介绍上传文件到服务器(多文件上传):
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
import org.apache.commons.fileupload.*;

public class upload extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节,这里设为2m
fu.setSizeMax(2*1024*1024);
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath("c://windows//temp");
//开始读取上传信息
List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
//正则匹配,过滤路径取文件名
String regExp=".+////(.+)$";
//过滤掉的文件类型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp<ERRORTYPE.LENGTH;TEMP++){
if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
//保存上传的文件到指定的目录
//在下文中上传文件至数据库时,将对这里改写
item.write(new File("d://" + m.group(1)));
out.print(name+" "+size+"");
}
catch(Exception e){
out.println(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
}
catch (IOException e){
out.println(e);
}
catch (FileUploadException e){
out.println(e);
}

}
}
现在介绍上传文件到服务器,下面只写出相关代码
以sql2000为例,表结构如下:
字段名:name filecode
类型: varchar image
数据库插入代码为:PreparedStatement pstmt=conn.prepareStatement("insert into test values(?,?)");
代码如下:
。。。。。。
try{
这段代码如果不去掉,将一同写入到服务器中
//item.write(new File("d://" + m.group(1)));

int byteread=0;
//读取输入流,也就是上传的文件内容
InputStream inStream=item.getInputStream();
pstmt.setString(1,m.group(1));
pstmt.setBinaryStream(2,inStream,(int)size);
pstmt.executeUpdate();
inStream.close();
out.println(name+" "+size+" ");
}
。。。。。。
这样就实现了上传文件至数据库

H. 怎么用java封装一个文件夹类,来实现发送文件夹功能

给你个思路
先通过File的isDirectory判断是否为文件夹如果是则创建文件夹,否则下载文件
创建文件夹后在寻找子文件夹中的内容
同样是上面的过程
可以思考使用递归来实现
其实文件夹上传
就是创建好文件夹在下载里面的文件,如果有子文件夹在创建子文件夹并下载子文件夹中的文件。
多半使用递归来实现

I. java怎么把文件上传到项目指定文件夹中知道的私聊我,奖励大大的

代码如下:

import java.io.*;
/**
* 复制文件夹或文件夹
*/
public class CopyDirectory {
// 源文件夹
static String url1 = "f:/photos";
// 目标文件夹
static String url2 = "d:/tempPhotos";
public static void main(String args[]) throws IOException {
// 创建目标文件夹
(new File(url2)).mkdirs();
// 获取源文件夹当前下的文件或目录
File[] file = (new File(url1)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 复制文件
File(file[i],new File(url2+file[i].getName()));
}
if (file[i].isDirectory()) {
// 复制目录
String sourceDir=url1+File.separator+file[i].getName();
String targetDir=url2+File.separator+file[i].getName();
Directiory(sourceDir, targetDir);
}
}
}
// 复制文件
public static void File(File sourceFile,File targetFile)
throws IOException{
// 新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);

// 新建文件输出流并对它进行缓冲
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);

// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();

//关闭流
inBuff.close();
outBuff.close();
output.close();
input.close();
}
// 复制文件夹
public static void Directiory(String sourceDir, String targetDir)
throws IOException {
// 新建目标目录
(new File(targetDir)).mkdirs();
// 获取源文件夹当前下的文件或目录
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 源文件
File sourceFile=file[i];
// 目标文件
File targetFile=new
File(new File(targetDir).getAbsolutePath()
+File.separator+file[i].getName());
File(sourceFile,targetFile);
}
if (file[i].isDirectory()) {
// 准备复制的源文件夹
String dir1=sourceDir + "/" + file[i].getName();
// 准备复制的目标文件夹
String dir2=targetDir + "/"+ file[i].getName();
Directiory(dir1, dir2);
}
}
}
}

J. 求JAVA文件夹上传解决方案

必须使用插件,单纯浏览器无法实现。

阅读全文

与java传输文件夹相关的资料

热点内容
iphone来电壁纸 浏览:40
删除文件夹找不到指定路径怎么办 浏览:487
原力大数据招聘 浏览:479
数据线圆头什么意思 浏览:768
协和app怎么取号 浏览:664
c坐标转换代码 浏览:707
唐筛数据为什么能看出男女 浏览:44
快手java 浏览:835
qq分享的文件在哪里 浏览:226
爱念电影 浏览:656
97不用下载播放器的 浏览:649
在线观看0855影视 浏览:489
女主叫向晚棠的小说 浏览:841
uglifyjs使用 浏览:328
西班牙最大寸度电影 浏览:641
孤寂之狼txt无删笔趣阁 浏览:895
微程序微指令微操作机器指令 浏览:370
百合小说下载 浏览:477
iphone7怎么新建文件夹 浏览:339
如何用复印机打印u盘文件 浏览:377

友情链接