实现文件上传功能,并在页面上显示上传的图片

struts.xml:


  
   
  
    
   /WEB-INF/jsp/day04/uploadform.jsp
  
  
   
    1024000
   
   
   
    /WEB-INF/jsp/day04/uploadimage.jsp
   
  
 


uploadform.jsp

<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>


	
		
	
	
		

文件上传





uploadimage.jsp

<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>


	
		
	
	
		

文件上传成功





UploadAction.java:

package day04;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.io.IOUtils;

import outman.BaseAction;


public class UploadAction extends BaseAction {
	private File some;
	private String someFileName;
	private String someContentType;
	private String imagePath;
	public String execute() throws Exception{
		System.out.println(some);
		System.out.println(some.length());
		System.out.println(someFileName);
		System.out.println(someContentType);
		String imageName = "file_" + System.currentTimeMillis()
		+ someFileName.substring(someFileName.lastIndexOf("."));
		System.out.println(imageName);
		imagePath = "upload_image/" + imageName;
		String realImagePath = toRealPath(imagePath);
		System.out.println(realImagePath);
		BufferedInputStream is = new BufferedInputStream(new FileInputStream(
				some));
		BufferedOutputStream os = new BufferedOutputStream(
				new FileOutputStream(realImagePath));
		IOUtils.copy(is, os);
		is.close();
		os.close();
		return "success";
	}
	public File getSome() {
		return some;
	}
	public void setSome(File some) {
		this.some = some;
	}
	public String getSomeFileName() {
		return someFileName;
	}
	public void setSomeFileName(String someFileName) {
		this.someFileName = someFileName;
	}
	public String getSomeContentType() {
		return someContentType;
	}
	public void setSomeContentType(String someContentType) {
		this.someContentType = someContentType;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
}

 

你可能感兴趣的:(重要的JAVA代码片断,string,interceptor,upload,file,action,html)