package com.born.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件处理
* @author asus
*
*/
public class FileUtil {
public static boolean copy(File src,File dest){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream(src));
bos=new BufferedOutputStream(new FileOutputStream(dest));
byte[] bts=new byte[1024];
int len=-1;
while((len=bis.read(bts))!=-1){
bos.write(bts,0,len);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.born.action;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import com.born.util.FileUtil;
/**
* 文件上传Action
*
* @author asus
*
*/
public class UploadAction {
/**
* 接收拦截器传入的临时文件
*/
private File some;
/**
* 接收拦截器注入的原始文件名
*/
private String someFileName;
public String Upload() {
if (some == null)
return "error";
// 将文件放于项目部署路径下的upload文件夹下
String path = "WEB-INF/jsp/" + someFileName;
// 根据相对部署路径计算完整路径
path = ServletActionContext.getServletContext().getRealPath(path);
// 将临时文件复制到上述路径下
FileUtil.copy(some, new File(path));
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;
}
}
<!--上传文件示例 -->
<package name="demo" namespace="/demo" extends="struts-default">
<!-- 打开上传文件页面 -->
<action name="toUpload">
<result name="success">/WEB-INF/jsp/upload.jsp</result>
</action>
<!--上传文件 -->
<action name="upload" class="com.born.action.UploadAction"
method="Upload">
<result name="success">/WEB-INF/jsp/ok.jsp</result>
<result name="error">/WEB-INF/jsp/error2.jsp</result>
</action>
</package>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- 上传文件对表单有2个要求
1,method=”post“
2,enctype="multipart/form-data"
-->
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="some" /> <input type="submit" value="提交" />
</form>
</body>
</html>