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>
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; } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|