首页

jsp 简单的文件上传

java

2020-6-28

package aaa.com.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//封装文件域
	private File file;
	//封装提交过来的文件名称
	private String fileFileName;
	//封装文件类型
	private String fileContenType;
	
	@Override
	public String execute() throws Exception {
		//通过上下文获取上传的文件的保存路径
		String root = ServletActionContext.getServletContext().getRealPath("/upload");
		//通过输入流读取要上传的文件
		InputStream is = new FileInputStream(file);
		//通过输出流写入到相对应的路径
		OutputStream os = new FileOutputStream(new File(root,fileFileName));
		//由于file是存放在临时文件夹下的文件,将文件名和路径打印出来
		System.out.println("=========================");
		System.out.println(fileFileName);
		System.out.println(file.getName());
		//字节流方式读取并写入
		byte[] buffer = new byte[500];
		int length = 0;
		//通过while循环把读取的东西写入
		while((length = is.read(buffer,0,buffer.length))!=-1){
			os.write(buffer);
		}
		os.close();
		is.close();
		return SUCCESS;
	}
	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;
	}

	public String getFileContenType() {
		return fileContenType;
	}
	public void setFileContenType(String fileContenType) {
		this.fileContenType = fileContenType;
	}
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
WenJanShangChuan/.classpath , 630
WenJanShangChuan/.mymetadata , 321
WenJanShangChuan/.mystrutsdata , 240
WenJanShangChuan/.project , 1842
WenJanShangChuan/.settings/.jsdtscope , 500
WenJanShangChuan/.settings/org.eclipse.jdt.core.prefs , 395
WenJanShangChuan/.settings/org.eclipse.wst.common.component , 486
WenJanShangChuan/.settings/org.eclipse.wst.common.project.facet.core.xml , 252
WenJanShangChuan/.settings/org.eclipse.wst.jsdt.ui.superType.container , 49
WenJanShangChuan/.settings/org.eclipse.wst.jsdt.ui.superType.name , 6
WenJanShangChuan/src/aaa/com/action/UploadAction.java , 1794
WenJanShangChuan/src/struts.xml , 387
WenJanShangChuan/WebRoot/META-INF/MANIFEST.MF , 36
WenJanShangChuan/WebRoot/success.jsp , 846
WenJanShangChuan/WebRoot/upload.jsp , 966
WenJanShangChuan/WebRoot/WEB-INF/classes/aaa/com/action/UploadAction.class , 2194
WenJanShangChuan/WebRoot/WEB-INF/classes/struts.xml , 387
WenJanShangChuan/WebRoot/WEB-INF/web.xml , 705
没有账号? 忘记密码?

社交账号快速登录