struts提供了<html:file>标签,集成了Apache的commons-upload工具包,上传文件变得更加简单,不再需要解析Request、遍历所有的输入域、判断是否为文件等。
struts对Apache的commons-upload进行了再封装,把上传文件封装成FormFile对象,直接获取该对象,将文件数据保存即可。
代码1:UploadForm.java
package com.strust1.test.actionFrom;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private FormFile file;
private String action;
private String text;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
代码2:upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%
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 '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>
<html:form action="/upload?action=upload" enctype="multipart/form-data">
文件:<html:file property="file" style="width:200px;"></html:file><br/>
备注:<html:textarea property="text" style="width:200px"></html:textarea><br/>
<html:submit value="开始上传"></html:submit>
</html:form>
</body>
</html>
注意:上传文件时Form表单要指定enctype为multipart/form-data,method一定要设置为POST。<html:form/>标签默认为POST。
FormFile并不是普通的文件,不能直接保存。FormFile提供getInputStream()接口,可以获得它的输入流,将它的内容保存到指定的文件中即可。
代码3:UploadAction.java
package com.strust1.test.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.strust1.test.actionFrom.UploadForm;
public class UploadAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadForm uploadForm = (UploadForm)form;
if("upload".equals(uploadForm.getAction())){
return upload(mapping, form, request, response);
}
return mapping.getInputForward();
}
public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
UploadForm uploadForm =(UploadForm)form;
StringBuffer buffer = new StringBuffer();
buffer.append("<style> body{font-size:12px;}</style>");
if(uploadForm.getFile() != null && uploadForm.getFile().getFileSize() > 0){
File classes = new File(getClass().getClassLoader().getResource("").getFile());//获得文件夹/WEB-INF/Classes
File uploadFolder = new File(classes.getParentFile().getParentFile() , "upload");//获取文件夹webRoot下的文件夹/upoad
uploadFolder.mkdirs();//创建文件夹
File file = new File(uploadFolder, uploadForm.getFile().getFileName());//保存到 /upload/下面
System.out.println(file.getPath());
OutputStream ous = null;
InputStream ins =null;
try{
byte[] b = new byte[1024];
int len = 0;
ins = uploadForm.getFile().getInputStream();
ous = new FileOutputStream(file);
while((len = ins.read(b)) != -1){
ous.write(b ,0 ,len);
}
}finally{
ins.close();
ous.close();
}
buffer.append("文件:<a href=upload/" + file.getName() + "target=_blank>" + file.getName() + "</a><br/>");
}else{
buffer.append("文件: 没有选择文件<br/>");
}
buffer.append("备注:" + uploadForm.getText() + "<br/>");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(buffer.toString());
return null;
}
}
文件保存在本Web应用的/upload文件夹下。
分享到:
相关推荐
struts1.2 简单文件上传例子(用eclipse3.4打开运行即可)
struts1.2实现动态多文件上传,可实现代码全奉献!
Struts1.2 实现的多文件上传下载。。。
struts1.2下实现文件上传进度条
struts1.2 图片文件上传(单个/动态多个) 单个的采用通用Action 需要ActionForm的,而动态多个上传是采用动态代理类(DispatchAction) 希望对大家有用,另JSP在IE测试通过,JS我就不再处理了!
采用Struts1.2实现简单的一个文件上传操作
struts1.2上传图片源代码(包含检查文件格式、限制文件大小功能); 已经做成成品,成功运行于项目之中
struts1.2 图片上传下载 源代码,注意在部署文件下建立upload文件夹存放上传图片的存放文件
单文件的demo。myeclipse6.0的工程
JAVA来上传下载文件,爱上大声大声大大硕大的
NULL 博文链接:https://fanth.iteye.com/blog/758778
由于上传限制,lib包不能上传,大家可以下载spring2.5版本的支持库(下最大的那个zip包),全部加进去就行了,还有mysql的连接包,项目很简单,直接导入eclipse,就可以使用,包内有mysql的sql文件,框架已经搭好,...
这是我结合网络资源。。和我对一些平时知识积累做的一个文件上传。。可能还存在漏洞。。请爱学者补全。。它将是一个完美的文件上传资料。。希望能给你的学习带来方便
OA办公自动化管理系统是一款基于Struts1.2、Hibernate3.0、Spring2和DWR的综合性企业级应用解决方案,旨在帮助企业实现高效、便捷的办公管理。该系统具备以下功能: 1. 用户管理:支持用户注册、登录、权限分配、...
struts1.2开发,可实习自定义,任意多个文件上传
由于上传空间的问题,JAR包不做上传,大家可以自己添加 spring 2.0.1 ,hibernate3.0,strutd1.2,相关JAR包; 及c3p0-0.9.1.2.jar, mysql-connector-java-5.1.10-bin.jar 。 当时写的时候用的是JDK1.4,没有在JDK1.5...
用Struts1.2实现文件上传功能,并且把文件保存在javaweb project的虚拟路径下。利用fileForm 类型上传二进制文件格式和字符串格式数据。
非常标准的文件上传功能源码,欢迎大家使用,希望能给你最大的帮助
Struts2升级到2.5.30,问题解决及过程记录, 文章详见:https://blog.csdn.net/sinat_36743893/article/details/124582101
asm-commons-3.3.jar,asm-tree-3.3.jar,commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.5.jar,freemarker-2.3.18.jar,javassist-3.11.0.GA.jar,ognl-3.0.4.jar,struts2-core-2.3.1.2....