ssh使用uploadify上传文件(2)xml及action

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,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; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇