struts2文件上专

文件上传工具类
public final class UploadFileUtil[size=medium][/size] {
public final static String FILE_UPLOAD_DIR = "/upload/";
public static String uploadFile(File file,String filePath,String fileName) throws IOException{
File dir=new File(filePath);
if(!dir.exists()){
//dir.mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
dir.mkdir();
}
//获得上传文件的输入流
InputStream is=new BufferedInputStream(new FileInputStream(file));
File fileSave=new File(filePath,fileName);
//创建上传文件的输出流
OutputStream os=new FileOutputStream(fileSave);
int length=0;
byte[] buffer=new byte[1024];
while((length=is.read(buffer,0,1024))!=-1){
os.write(buffer, 0,1024);
}
os.close();
is.close();
return fileName;
}
}
Action类
public class Jkda01 implements Action {
private File pic;
private String picUrl;
private String picFileName;
private String picContentType;

public Jkda01() {

}

public File getPic() {
return pic;
}

public void setPic(File pic) {
this.pic = pic;
}

public String getPicUrl() {
return picUrl;
}

public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}

public String getPicFileName() {
return picFileName;
}

public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}

public String getPicContentType() {
return picContentType;
}

public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}

public String upload() {
if (pic != null && pic.length() != 0) {
System.out.println(pic);
System.out.println(picFileName);
System.out.println(picContentType);
ServletContext sc = ServletActionContext.getServletContext();
try {
UploadFileUtil.uploadFile(pic, sc
.getRealPath(UploadFileUtil.FILE_UPLOAD_DIR),
picFileName);
System.out.println("url:" + UploadFileUtil.FILE_UPLOAD_DIR
+ picFileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件上传异常:" + e.getMessage());
}
}
return "upload";
}
}
struts2的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="upload" class="com.xyh.jkda.action.Jkda01">
<!--maximumSize 上传大小allowedTypes 上传类型-->
<param name="maximumSize">10240000000</param>
<param name="allowedTypes"> image/pjpeg,image/gif,image/bmp,image/x-png</param>
<interceptor-ref name="defaultStack" />
<result name="upload">upload.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新益华注册首页</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>
<s:form action="upload!upload" enctype="multipart/form-data" method="post">
<s:textfield name="fileNamel" label="文件名称:"></s:textfield>
<s:file name="pic" contentEditable="false"></s:file>
<s:submit></s:submit>
</s:form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值