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

struts.xml:

<package name="advance" extends="struts-default"
  namespace="/day04">
  <interceptors>
   <interceptor name="some" class="interceptor.SomeInterceptor"/>
  </interceptors>
    <action name="uploadform" >
   <result name="success">/WEB-INF/jsp/day04/uploadform.jsp</result>
  </action>
  <action name="upload" class="day04.UploadAction">
   <interceptor-ref name="fileUpload">
    <param name="maximumSize">1024000</param>
   </interceptor-ref>
   <interceptor-ref name="basicStack" />
   <result name="success">
    /WEB-INF/jsp/day04/uploadimage.jsp
   </result>
  </action>
 </package>


uploadform.jsp

<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title></title>
	</head>
	<body>
		<h1>
			文件上传
		</h1>
		<s:form action="upload" method="post" theme="xhtml"
			enctype="multipart/form-data">
			<s:file label="File" name="some" />
			<s:submit value="提交" />
		</s:form>
		<br />
		<br />
		<br />
	</body>
</html>


uploadimage.jsp

<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title></title>
	</head>
	<body>
		<h1>
			文件上传成功
		</h1>
		<img src="../${imagePath}" />
		<br />
		<br />
		<br />
	</body>
</html>


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;
	}
}

 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭