ssh使用uploadify上传文件(2)xml及action
2014-11-12 17:50
381 查看
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="2000000" />
<package name="lottery" extends="struts-default" namespace="/lottery">
<!-- session过滤器开始 -->
<interceptors>
<interceptor class="com.tp.common.util.AuthorityInterceptor"
name="authStack" />
<interceptor name="codeTabActionInterceptor" class="tp_ac_codeTabActionInterceptor" />
<interceptor-stack name="authStackInterceptor">
<interceptor-ref name="authStack" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack name="tp_ac_codeTabActionInterceptor_Stack">
<interceptor-ref name="codeTabActionInterceptor"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception"
exception="com.tp.common.exception.BusinessException">
</exception-mapping>
<exception-mapping result="exception"
exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="uploadFile" class="lotteryAction" method="uploadFile">
</action>
</package>
</struts>
action:
private static final int BUFFER_SIZE = 16 * 1024;
private File uploadImage;
private String uploadImageContentType;
private String uploadImageFileName;
/**
*
*〈上传图片〉
* @return String
*/
public String uploadFile() throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String flag = request.getParameter("flag");
File srcFiles = null;
String fileName =null;
if(flag.equals("start")){
srcFiles = this.getUploadImage();
fileName = this.getUploadImageFileName();
}else{
srcFiles = this.getEnduploadImage();
fileName = this.getEnduploadImageFileName();
}
Long fileSize = 0L;
if(srcFiles != null){
fileSize = srcFiles.length();
}
List<String> successFileList = new ArrayList<String>();
Long testtime = System.currentTimeMillis();
SimpleDateFormat fm2 = new SimpleDateFormat("yyyyMMdd");
String folder = fm2.format(testtime);
String filepath = request.getRealPath("/BPO/UpLoad/weixin/") +"/"+ folder + "/";
if (!new java.io.File(filepath).exists())
{
new java.io.File(filepath).mkdirs();
}
SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddhhmmsss");
String filename = fm.format(testtime);
String fileExt = fileName.substring(fileName.lastIndexOf("."));
response.setContentType("text/html; charset=UTF-8");
StringBuffer buffer=new StringBuffer();
File dstFile = new File(filepath+"\\"+filename+fileExt);
if (copy(srcFiles, dstFile)) {
successFileList.add(getUploadImageFileName());
}
request.setAttribute("successFileList", successFileList);
request.setAttribute("dstFile", dstFile);
String url ="/BPO/UpLoad/weixin/"+ folder + "/"+filename+fileExt;
response.getWriter().print(dstFile+"*"+url);//向页面端返回结果信息
return null;
}
// 自己封装的一个把源文件对象复制成目标文件对象
private static boolean copy(File src, File dst) {
boolean result = false;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="2000000" />
<package name="lottery" extends="struts-default" namespace="/lottery">
<!-- session过滤器开始 -->
<interceptors>
<interceptor class="com.tp.common.util.AuthorityInterceptor"
name="authStack" />
<interceptor name="codeTabActionInterceptor" class="tp_ac_codeTabActionInterceptor" />
<interceptor-stack name="authStackInterceptor">
<interceptor-ref name="authStack" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack name="tp_ac_codeTabActionInterceptor_Stack">
<interceptor-ref name="codeTabActionInterceptor"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception"
exception="com.tp.common.exception.BusinessException">
</exception-mapping>
<exception-mapping result="exception"
exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="uploadFile" class="lotteryAction" method="uploadFile">
</action>
</package>
</struts>
action:
private static final int BUFFER_SIZE = 16 * 1024;
private File uploadImage;
private String uploadImageContentType;
private String uploadImageFileName;
/**
*
*〈上传图片〉
* @return String
*/
public String uploadFile() throws Exception
{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String flag = request.getParameter("flag");
File srcFiles = null;
String fileName =null;
if(flag.equals("start")){
srcFiles = this.getUploadImage();
fileName = this.getUploadImageFileName();
}else{
srcFiles = this.getEnduploadImage();
fileName = this.getEnduploadImageFileName();
}
Long fileSize = 0L;
if(srcFiles != null){
fileSize = srcFiles.length();
}
List<String> successFileList = new ArrayList<String>();
Long testtime = System.currentTimeMillis();
SimpleDateFormat fm2 = new SimpleDateFormat("yyyyMMdd");
String folder = fm2.format(testtime);
String filepath = request.getRealPath("/BPO/UpLoad/weixin/") +"/"+ folder + "/";
if (!new java.io.File(filepath).exists())
{
new java.io.File(filepath).mkdirs();
}
SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddhhmmsss");
String filename = fm.format(testtime);
String fileExt = fileName.substring(fileName.lastIndexOf("."));
response.setContentType("text/html; charset=UTF-8");
StringBuffer buffer=new StringBuffer();
File dstFile = new File(filepath+"\\"+filename+fileExt);
if (copy(srcFiles, dstFile)) {
successFileList.add(getUploadImageFileName());
}
request.setAttribute("successFileList", successFileList);
request.setAttribute("dstFile", dstFile);
String url ="/BPO/UpLoad/weixin/"+ folder + "/"+filename+fileExt;
response.getWriter().print(dstFile+"*"+url);//向页面端返回结果信息
return null;
}
// 自己封装的一个把源文件对象复制成目标文件对象
private static boolean copy(File src, File dst) {
boolean result = false;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
相关文章推荐
- ssh使用uploadify上传文件(1)jsp
- 在Struts2中使用Uploadify组件上传文件
- 基于jquery+flash的文件上传组件uploadify使用
- 上传文件插件 Uploadify使用说明
- django使用uploadify上传文件
- 在asp.net mvc中使用Uploadify上传文件
- ssh项目使用uploadify-v2.1.4上传插件出现诡异问题(session丢失、初始化请求)
- Web开发中的文件上传组件uploadify的使用
- 上传文件插件 Uploadify使用说明 转
- 使用XML上传文件
- 在asp.net mvc中使用Uploadify上传文件
- 使用Javascript XMLHttpRequest模拟表单(Form)提交上传文件
- 使用XML帮助上传文件
- 在struts.xml文件中使用spring代理,管理action
- fileupload文件上传(配合Uploadify使用)
- 使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件
- 在asp.net mvc中使用Uploadify上传文件
- 使用XML帮助上传文件
- asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
- 使用 jquery 的 上传文件插件 uploadify 3.1 配合 java 来做一个简单的文件上次功能。并且在界面上有radio 的选择内容也要上传