在stucts中上传文件步骤:
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:编写提交表单:index.jsp
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/test/upload.action" method="post">
<input type="file" name="uploadImage">
<input type="submit" value="提交">
</form>
<body>
</body>
</html>
第三步:编写Action
public class uplodeFile {
private File uploadImage;//这个名字和<input type="file" name="uploadImage">相同
private String uplodeFileContentType;//同上-ContentType这必须这么写
private String uploadImageFileName;//同上-FileName;这必须这么写
private String path;
public String getPath() {
return path;
}
public File getUploadImage() {
return uploadImage;
}
public void setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUplodeFileContentType() {
return uplodeFileContentType;
}
public void setUplodeFileContentType(String uplodeFileContentType) {
this.uplodeFileContentType = uplodeFileContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}
public String upload() throws Exception
{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
path=file.getAbsolutePath();
FileUtils.copyFile(uploadImage, new File(file, uploadImageFileName));
return "success";
}
}
第四步:配置struts.xml文件
<action name="upload" class="com.liyong.uplodeFile.uplodeFile" method="upload" >
<result name="success">/WEB-INF/page/success.jsp</result>
<!-- 访问路径 http://localhost:8080/Structs2/test/sevletScope -->
</action>
第五步:部署
第六步:访问:http://localhost:8080/Structs2/index.jsp
注意:index.jsp在WebRoot目录下
文件上传
原创
©著作权归作者所有:来自51CTO博客作者青年IT男1的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
文件上传并展示上传文件
1、问题背景 利用文件上传组件file,上传文件后并显示文件(图片)2、实现源码 文件上传并展示文件路径 3、实现结果 ...
html 文件上传 javascript jquery 显示文件 -
文件上传 - iframe上传
File Upload
学习 html 表单 javascript 数据