struts2单个文件上传
form表单:
<form action="<%=basePath%>upload!upload.action"
enctype="multipart/form-data" method="post">
选择文件<input type="file" name="file" /> <input type="submit"
value="上传文件" />
</form>
action类:
private File file;
private String fileFileName;
private String uploadfileContentType;
public String upload() throws IOException {
String realpath = ServletActionContext.getServletContext().getRealPath(
"/upload"); //获得路径
if (file != null) {
File savefile = new File(new File(realpath), fileFileName);//获得文件地址
if (!savefile.getParentFile().exists()) //判断文件是否存在
savefile.getParentFile().mkdirs(); //创建目录
FileUtils.copyFile(file, savefile); //保存文件
ActionContext.getContext().put("message", "文件上传成功"); 把message信息传送到页面上
return "success";
}
return "fail";
}
接收信息页面上:
<body>
${message}
</body>
struts.xml配置略
form表单:
<form action="<%=basePath%>upload!upload.action"
enctype="multipart/form-data" method="post">
选择文件<input type="file" name="file" /> <input type="submit"
value="上传文件" />
</form>
action类:
private File file;
private String fileFileName;
private String uploadfileContentType;
public String upload() throws IOException {
String realpath = ServletActionContext.getServletContext().getRealPath(
"/upload"); //获得路径
if (file != null) {
File savefile = new File(new File(realpath), fileFileName);//获得文件地址
if (!savefile.getParentFile().exists()) //判断文件是否存在
savefile.getParentFile().mkdirs(); //创建目录
FileUtils.copyFile(file, savefile); //保存文件
ActionContext.getContext().put("message", "文件上传成功"); 把message信息传送到页面上
return "success";
}
return "fail";
}
接收信息页面上:
<body>
${message}
</body>
struts.xml配置略
版权声明:本文为博主原创文章,未经博主允许不得转载。
来源:https://www.cnblogs.com/shipeng22022/archive/2013/05/08/4614073.html