`
huangfeng555
  • 浏览: 22391 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

图片文件上传

    博客分类:
  • JAVA
阅读更多
  文件的上传其实不用加COPY()方法,在Struts文件中已经设置了上传文件的位置为才C:\目录下,COPY()方法起到了把文件复制到指定的文件目录。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UpLoadAction extends ActionSupport {
	private static final long serialVersionUID = 572146812454l;
	private static final int BUFFER_SIZE = 16 * 1024;
	private File myFile;
	private String imageFileName;
	/**
	 * 初始化页面
	 */
	@Override
	public String execute() {
		return "input";
	}
	
	
	/**
	 * 上传照片
	 * @return
	 */
	public String upload(){
				imageFileName = new Date().getTime() +Math.random()+".jpg";
				File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/Images") + "/" + imageFileName);
				copy(myFile, imageFile);
		return "success";
	}
	
	/**
	 * copy图片文件
	 * @param src
	 * @param dst
	 */
	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getImageFileName() {
		return imageFileName;
	}

}





<%@ page language ="java" contentType ="text/html;charset=utf-8"%> 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
body {
	margin-left: 0px;
	margin-top: 0px;
	background-color:#ECF8FF;
}
</style></head> 
<body>
<form action ="../HR_WEB/UpLoadAction!upload.action" method ="POST" enctype ="multipart/form-data">  
<table width="179" height="188" border="0" cellpadding="0" cellspacing="0" >
<tr>
          <td  align="center" bgcolor="#ECF8FF"><img name="照片"  src ='<%=request.getContextPath() %>/Images/<s:property value ="imageFileName" /> ' width="90" height="150" /></td>
    </tr>
        <tr>
          <td align="center" bgcolor="#ECF8FF"><input name="myFile" type="file" size="15"/></td>
    </tr>
        <tr>
          <td align="center" bgcolor="#ECF8FF"><input type="submit"  value="上传"/></td>
    </tr>
      </table>
</form>
</body> 
</html>



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.multipart.saveDir" value="c:\"></constant>
	<package name="HR_WEB" extends="struts-default" namespace="/HR_WEB">


		<!-- 照片上传 -->
		<action name="UpLoadAction" class="hr.recruitment.action.UpLoadAction">
			<result name="input">/recruit/FileUpload.jsp</result>
			<result name="success">/recruit/FileUpload.jsp</result>
		</action>
	</package>
</struts>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics