首页>代码>java ssh文件上传精确计算每个上传文件大小并显示进度>/SSH2Upload/src/org/yzsoft/upload/action/UploadAction.java
package org.yzsoft.upload.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
 
public class UploadAction extends ActionSupport { 
	private static final int BUFFER_SIZE = 0;
	
	private File file;
	
	private static String ext; 
	private static String fileFileName;
	private static float percent = 0;
	
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
 

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	} 
	 

	private static void copy(File src, File dst) { 
		InputStream in = null;
		OutputStream out = null;
		float completedSize = 0;
		float fileSize = 0;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
			fileSize = in.available();
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
				completedSize += (long) len;
				percent = completedSize / fileSize * 100;
			} 
		} catch (Exception e) {
			System.out.println(e); 
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					System.out.println(e);
				}
			}
		} 
	}
	
	public String sumPre() { 
		try { 
			if (percent >= 99.9) {
				percent = 0;
			}
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			System.out.println(getFileFileName() + "                                             filename"); 
			response.getWriter().print(percent);
			//out.print(percent);
			System.out.println(percent);
		} catch (IOException e) {
			System.out.println("异常:" + e);
		}
		return null;
	}
	
	//上传的方法
	public String upload() { 
		HttpServletRequest request = ServletActionContext.getRequest();
		///////////////////////// 获得客户端IP //////////////////////////
		//System.out.println(request.getRemoteAddr());
//		String ip = request.getHeader("X-Forwarded-For");
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//		ip = request.getHeader("Proxy-Client-IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//		ip = request.getHeader("WL-Proxy-Client-IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//		ip = request.getHeader("HTTP_CLIENT_IP");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//		ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//		}
//		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//		ip = request.getRemoteAddr();
//		}
//		System.out.println(ip);
//		System.out.println(ip);
//		System.out.println(ip);
//		System.out.println(ip);
//		System.out.println(ip);
//		System.out.println(ip);
//		System.out.println(ip);
		//////////////////////// 获得客户端IP ///////////////////////////
		
		
		try {
	
			File srcfile = this.getFile();
			
			String uploadPath = ServletActionContext.getServletContext().getRealPath("upload");// 上传路径 
			ext = fileFileName.substring(fileFileName.lastIndexOf(".")).toLowerCase();// 取得后缀
			System.out.println("取得后缀        " + ext); 
			File dstFile = new File(uploadPath, fileFileName); 
			copy(srcfile, dstFile);
		} catch (Exception e) {
			System.out.println(e);
		}
		return "success";

	}  
	/**************/
	
	public static void main(String []args){
		System.out.println("fuck you!!");
		
	}
  
	
}
最近下载更多
1358849392  LV21 2024年4月12日
微信网友_6166561027215360  LV2 2022年10月8日
仰望星空five  LV1 2020年3月13日
skipple3  LV39 2020年3月3日
yang9999  LV1 2019年12月20日
lgp1991  LV1 2019年5月7日
快乐的男孩子  LV5 2019年1月23日
caohanren  LV11 2019年1月17日
Silence丶二哈  LV13 2019年1月16日
tian2019  LV1 2019年1月10日
最近浏览更多
1358849392  LV21 2024年4月12日
ming_123_9715  LV23 2022年12月15日
wanglun_wl  LV10 2022年10月18日
微信网友_6166561027215360  LV2 2022年10月8日
funcrit  LV2 2022年7月18日
zhy1989wz  LV7 2022年3月15日
农村拓哉 2021年12月7日
暂无贡献等级
13561143836  LV7 2021年11月10日
明天更美好  LV10 2021年9月9日
铁血战士  LV1 2021年6月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友