java上传客户端文件
㈠ java 怎么在客户端把客户端一个文件上传到web服务器上
第一步 表单 type="file"
第二步 结合structs
㈡ java客户端怎么把本地的文件上传到服务器
String realpath = ServletActionContext.getServletContext().getRealPath("/upload") ;//获取服务器路径
String[] targetFileName = uploadFileName;
for (int i = 0; i < upload.length; i++) {
File target = new File(realpath, targetFileName[i]);
FileUtils.File(upload[i], target);
//这是一个文件复制类File()里面就是IO操作,如果你不用这个类也可以自己写一个IO复制文件的类
}
其中private File[] upload;// 实际上传文件
private String[] uploadContentType; // 文件的内容类型
private String[] uploadFileName; // 上传文件名
这三个参数必须这样命名,因为文件上传控件默认是封装了这3个参数的,且在action里面他们应有get,set方法
㈢ Java客户端通过Http发送POST请求上传文件
要按http的multi-part上传的。接收端,再按multi-part解析成文件流
㈣ java客户端通过http发送POST请求上传文件
这样的写法,是直接socket的做法。
如果是HTTP的,要按HTTP的协议进行。先了解一下multi-part的post
㈤ java文件上传服务器缓存
Java文件上传服务器缓存指的是在将文件上传到服务器之前,将文件先缓存在本地,以便提高上传速度和减少网络流量的过程。这样可以有效地提高文件上传的效率,减少服务器的负载。
㈥ 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){}
}
}
}