文件上传

本文介绍了一个使用Struts2框架实现文件上传的例子。通过定义Action类`UploadAction`来处理文件上传逻辑,并利用`FileUtil`工具类完成文件复制。此外,还展示了相应的配置文件及上传页面的实现。

package com.born.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 文件处理
* @author asus
*
*/
public class FileUtil {
public static boolean copy(File src,File dest){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream(src));
bos=new BufferedOutputStream(new FileOutputStream(dest));
byte[] bts=new byte[1024];
int len=-1;
while((len=bis.read(bts))!=-1){
bos.write(bts,0,len);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

 

 

 

 

package com.born.action;

import java.io.File;

import org.apache.struts2.ServletActionContext;

import com.born.util.FileUtil;

/**
* 文件上传Action
*
* @author asus
*
*/
public class UploadAction {
/**
* 接收拦截器传入的临时文件
*/
private File some;
/**
* 接收拦截器注入的原始文件名
*/
private String someFileName;

public String Upload() {
if (some == null)
return "error";
// 将文件放于项目部署路径下的upload文件夹下
String path = "WEB-INF/jsp/" + someFileName;
// 根据相对部署路径计算完整路径
path = ServletActionContext.getServletContext().getRealPath(path);
// 将临时文件复制到上述路径下
FileUtil.copy(some, new File(path));

return "success";
}

public File getSome() {
return some;
}

public void setSome(File some) {
this.some = some;
}

public String getSomeFileName() {
return someFileName;
}

public void setSomeFileName(String someFileName) {
this.someFileName = someFileName;
}

}

 

 

 

<!--上传文件示例 -->
<package name="demo" namespace="/demo" extends="struts-default">
<!-- 打开上传文件页面 -->
<action name="toUpload">
<result name="success">/WEB-INF/jsp/upload.jsp</result>
</action>
<!--上传文件 -->
<action name="upload" class="com.born.action.UploadAction"
method="Upload">
<result name="success">/WEB-INF/jsp/ok.jsp</result>
<result name="error">/WEB-INF/jsp/error2.jsp</result>
</action>
</package>

 

 

 

 

<%@ 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>


<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>
<!-- 上传文件对表单有2个要求
1,method=”post“
2,enctype="multipart/form-data"
-->
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="file" name="some" /> <input type="submit" value="提交" />
</form>
</body>
</html>

转载于:https://www.cnblogs.com/xuehen/p/4292684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值