package com.tch.action;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.tch.util.FileCopyUtil;
public class FileUploadAction
{
// 注意这些变量名,看见了没? 和Struts2的FormBean是一样的。
// 要和客户端添加字段时的关键字保持一致!!
private String fileName = null;
private String fileData = null;
//针对每个字段的关键字,下面的这些Set方法必须一个不少,否则你什么也得不到
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setFileData(String fileData) {
this.fileData = fileData;
}
public String upload()
{
String webappPath = findServerPath();
/*几种获取tomcat下webapps路径的方法:
一、web项目根目录获取D:\apache-tomcat-6.0.36\webapps\XXXX项目名\
String path = ServletActionContext.getServletContext().getRealPath("/");
二、项目相对路径/XXXX项目名
String appPath = ServletActionContext.getServletContext().getContextPath();
三、D:\apache-tomcat-6.0.36\bin把bin文件夹变到 webapps文件里面
nowpath=System.getProperty("user.dir");
tempdir=nowpath.replace("bin", "webapps");
四、获取类全路径,截取见findServerPath
*/
//读取web.xml中的设置:保存上传图片的路径
String chatFile_path = ServletActionContext.getServletContext().getInitParameter("UPLOAD_CHAT_FILE_PATH");
FileCopyUtil util = new FileCopyUtil();
String[] array = fileName.split("\\.");
//取得文件类型如jpg、png
String fileType = array[array.length-1];
//拼接得到图片保证的全路径
String destDir = webappPath + chatFile_path;
//先获取HttpServletResponse,保存返回信息
HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get( ServletActionContext.HTTP_RESPONSE );
//打开数据流,把要给客户端返回的数据写进数据流
OutputStream writer = null;
try{
//此时debug调试,可看到fileData为保存的缓存文件的绝对路径。格式为:X:/xxx/xxx/xxx/xxx_xxx_xxx.tmp
//上传任务已完成,只需要调用工具类,将临时文件拷贝到指定目录,并修改后缀名
String newFilePath = util.copyFile(fileType, fileData, destDir);
int index = newFilePath.indexOf("webapps");
String path = newFilePath.substring(index+7);
writer = response.getOutputStream();
//将需要的信息,写入response,供客户端使用。
writer.write(path.getBytes());
}
catch( Exception e ) {
e.printStackTrace();
}
finally{
if( writer != null ) {
try {
writer.close();
}
catch( Exception e ) {
}
}
}
// 这里不用再返回其他字符串了。
// 客户端接收的不是这里返回的数据,这个是显示结果网页才会用到的返回值。
return null;
}
private String findServerPath()
{
String classPath = this.getClass().getClassLoader().getResource("/").getPath();
try {
classPath =URLDecoder.decode(classPath, "gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int i = classPath.indexOf("webapps");
String path = classPath.substring(1,i+7);
return path;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
upload.zip (32个子文件)
upload
.project 1KB
.settings
.jsdtscope 500B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 291B
WebRoot
WEB-INF
web.xml 870B
lib
javassist-3.11.0.GA.jar 600KB
httpcore-4.2.2.jar 218KB
xwork-core-2.2.3.1.jar 602KB
commons-lang-2.6.jar 278KB
commons-io-2.0.1.jar 156KB
commons-fileupload-1.2.2.jar 58KB
freemarker-2.3.16.jar 860KB
log4j-1.2.17.jar 478KB
ognl-3.0.1.jar 224KB
httpmime-4.2.3.jar 26KB
httpclient-cache-4.2.3.jar 113KB
httpclient-4.2.3.jar 423KB
commons-logging-1.1.1.jar 59KB
struts2-core-2.2.3.1.jar 756KB
classes
com
tch
Test.class 3KB
action
FileUploadAction.class 3KB
util
FileCopyUtil.class 2KB
struts.xml 407B
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
src
com
tch
action
FileUploadAction.java 4KB
Test.java 3KB
util
FileCopyUtil.java 1KB
struts.xml 407B
.classpath 1KB
共 32 条
- 1
断更ing~
- 粉丝: 12
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页