老师说struts2文件上传而已,你看下面。

[size=large][color=red]1.文件上传的页面upload.jsp。[/color][/size]

<%@ 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>文件上传</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">
</head>

<body>
<form action="user_test" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/>
<br/>
<input type="submit" value="submit">
</form>
</body>
</html>

[color=red]注意:[/color]
[size=large][color=green]1.文件上传操作表单的method必须为post。
2.表单数据编码的enctype为multipart/form-data
3.记住file的name为file一会说它。
[/color][/size]
2.struts2文件上传默认使用的是apache commons的FileUpload。
action层的代码这个样子的:

public class UserAction extends ActionSupport {

/**文件上传相关属性**/
private File file;//前台表单名字需叫file
private String fileFileName;
private String fileContentType;
//setter and getter 此处省略了
//上传的主要方法
@SuppressWarnings("deprecation")
protected boolean upload(){
boolean flag = false;
InputStream is = null;
OutputStream os = null;
try{
is = new FileInputStream(file);
String root = ServletActionContext.getRequest().getRealPath("/upload");//文件保存的路径
File destFile = new File(root,this.getFileFileName());
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length =0;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
flag = true;
}catch(Exception ex){
flag = false;
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
//do nothing
}

}
return flag;

}



}
/**
*前台表单提交到这里
*/
public String test(){
if(upload()){
successPath = "/upload/uploadresult.jsp";
return SUCCESS;
}else{
successPath = "/upload/uploadtest.jsp";
return SUCCESS;
}

}


[color=red]注意:
[/color][size=large][color=green]1.文件上传的相关属性那里,页面file的name是file,
这里就定义一个file,fileFileName,fileContentType。
如果页面file的name为abc,
这里就定义一个abc,abcFileName,abcContentType。[/color][/size]

3。这里以图片为例子显示上传的图片页面uploadresult.jsp。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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">
</head>
<body>
<img src="<%=request.getContextPath() %>/upload/<s:property value="fileFileName"/>" />
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值