fileupload.java

来自「关于Struts2的初体验」· Java 代码 · 共 75 行

JAVA
75
字号
package com.test.bean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUpload {

	public boolean fileUpload(File file, String root, String fileFileName) {

		InputStream is = null;

		OutputStream os = null;

		File folder = null;
		
		boolean flag = false;

		// 确定文件上传的资源
		try {

			is = new FileInputStream(file);

		} catch (FileNotFoundException e) {
			
			e.printStackTrace();

		}
		
		// 上传的位置
		folder = new File(root, fileFileName);
		
		// 准备上传资源
		try {
			
			os = new FileOutputStream(folder);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		// 开始上传资源
		byte[] buffer = new byte[1024];
		
		int length = 0;
		
		try {
			while ((length = is.read(buffer)) > 0) {
				
				os.write(buffer, 0, length);
				
			}
			
			os.close();
			is.close();
			
			flag = true;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?