图片文件上传

[i] 文件的上传其实不用加COPY()方法,在Struts文件中已经设置了上传文件的位置为才C:\目录下,COPY()方法起到了把文件复制到指定的文件目录。[/i]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值