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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android+struts2实现文件图片上传
共32个文件
jar:14个
java:3个
class:3个
4星 · 超过85%的资源 需积分: 10 25 下载量 17 浏览量
2016-08-13
10:25:53
上传
评论
收藏 4.32MB ZIP 举报
温馨提示
android实现文件上传、服务端struts2实现。简单易用、注释详细。
资源推荐
资源详情
资源评论
收起资源包目录
upload.zip (32个子文件)
upload
.project 1KB
.mymetadata 291B
src
struts.xml 407B
com
tch
action
FileUploadAction.java 4KB
Test.java 3KB
util
FileCopyUtil.java 1KB
WebRoot
WEB-INF
classes
struts.xml 407B
com
tch
action
FileUploadAction.class 3KB
util
FileCopyUtil.class 2KB
Test.class 3KB
lib
struts2-core-2.2.3.1.jar 756KB
httpcore-4.2.2.jar 218KB
commons-logging-1.1.1.jar 59KB
log4j-1.2.17.jar 478KB
javassist-3.11.0.GA.jar 600KB
commons-io-2.0.1.jar 156KB
httpclient-4.2.3.jar 423KB
commons-fileupload-1.2.2.jar 58KB
freemarker-2.3.16.jar 860KB
xwork-core-2.2.3.1.jar 602KB
httpmime-4.2.3.jar 26KB
commons-lang-2.6.jar 278KB
httpclient-cache-4.2.3.jar 113KB
ognl-3.0.1.jar 224KB
web.xml 870B
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.classpath 1KB
共 32 条
- 1
资源评论
- ycxylrh2018-04-27没用得上,谢谢!
黑色小五子
- 粉丝: 4
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功