BaseAction
上传用的BaseAction
上传时所用到的拦截配置
- package com.easyjob.cnhuike.actions;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts2.ServletActionContext;
- import org.springframework.context.ApplicationContext;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import com.easyjob.cnhuike.util.modelConstant.LoginConstant;
- import com.framework.util.DateUtil;
- import com.opensymphony.xwork2.ActionSupport;
- public class BaseAction extends ActionSupport {
- public static final String LIST="list";
- public static final String NEXT="next";
- String inputAction;
- String nextAction;
- public HttpServletRequest getRequest(){
- return ServletActionContext.getRequest();
- }
- public HttpServletResponse getResponse(){
- return ServletActionContext.getResponse();
- }
- public HttpSession getSession(){
- return getRequest().getSession();
- }
- public ServletContext getServletContext(){
- return ServletActionContext.getServletContext();
- }
- public String getRealyPath(String path){
- return getServletContext().getRealPath(path);
- }
- public String getInputAction() {
- return inputAction;
- }
- public void setInputAction(String inputAction) {
- this.inputAction = inputAction;
- }
- public String getNextAction() {
- return nextAction;
- }
- public void setNextAction(String nextAction) {
- this.nextAction = nextAction;
- }
- }
上传用的BaseAction
- package com.easyjob.cnhuike.actions;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- import com.framework.util.DateUtil;
- public class BaseUploadFileAction extends BaseAction {
- public static final int BUFFER_SIZE=16*1024;
- // 用File数组来封装多个上传文件域对象
- public File[] upload;
- // 用String数组来封装多个上传文件名
- public String[] uploadFileName;
- // 用String数组来封装多个上传文件类型
- public String[] uploadContentType;
- // 保存文件的目录路径(通过依赖注入)
- public String savePath="";
- public BaseUploadFileAction(){
- savePath="\\uploadFile";
- }
- //文件复制
- public boolean copy(File src, File dst,int BUFFER_SIZE) {
- 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;
- }
- //上传文件方法
- public List<String> uploadFile(String path,boolean isFullPath) throws IOException{
- List<String> successFileList=new ArrayList<String>();
- // 处理每个要上传的文件
- for (int i = 0; i < upload.length; i++) {
- // 根据服务器的文件保存地址和原文件名创建目录文件全路径
- String srcFilesInfo=uploadFileName[i].toString();
- //取得文件的后缀
- String FileExtensions=srcFilesInfo.substring(srcFilesInfo.lastIndexOf("."),srcFilesInfo.length());
- String fileName=DateUtil.getNowDate(null)+"-"+DateUtil.getHour()+DateUtil.getMinute()+DateUtil.getSecond()+i+FileExtensions;
- String dstPath = getRealyPath(path) + "\\"+fileName;
- File dstFile = new File(dstPath);
- if(copy(upload[i], dstFile,BUFFER_SIZE)){
- if(isFullPath){
- successFileList.add(path+fileName);
- }else{
- successFileList.add(fileName);
- }
- }
- if(successFileList.size()<1){
- throw new IOException();
- }
- }
- return successFileList;
- }
- public List<String> uploadFile(String path) throws IOException{
- return uploadFile(path,false);
- }
- //取得路径中的文件名
- public List<String> getFileNames(String path){
- File file=new File(getRealyPath(path));
- List<String> resultList=new ArrayList<String>();
- File[] files= file.listFiles();
- for (File tempFile : files) {
- if(tempFile.isFile()){
- resultList.add(tempFile.getName());
- }
- }
- return resultList;
- }
- //建立文件夹
- public void createFold(String path){
- try{
- path=getRealyPath(path);
- File folder = new File(path);
- if(!folder.exists()){
- folder.mkdirs();
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- /*
- *geter and seter
- *
- * */
- public File[] getUpload() {
- return upload;
- }
- public void setUpload(File[] upload) {
- this.upload = upload;
- }
- public String[] getUploadFileName() {
- return uploadFileName;
- }
- public void setUploadFileName(String[] uploadFileName) {
- this.uploadFileName = uploadFileName;
- }
- public String[] getUploadContentType() {
- return uploadContentType;
- }
- public void setUploadContentType(String[] uploadContentType) {
- this.uploadContentType = uploadContentType;
- }
- public String getSavePath() {
- return savePath;
- }
- public void setSavePath(String savePath) {
- this.savePath = savePath;
- }
- public static int getBUFFER_SIZE() {
- return BUFFER_SIZE;
- }
- }
上传时所用到的拦截配置
- <interceptors>
- <interceptor-stack name="albumUpload">
- <interceptor-ref name="fileUpload">
- <!-- 配置允许上传的文件类型,多个用","分隔 -->
- <param name="allowedTypes">
- image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
- </param>
- <!-- 配置允许上传的文件大小,单位字节 -->
- <param name="maximumSize">10240000</param>
- </interceptor-ref>
- <interceptor-ref name="defaultStack" />
- </interceptor-stack>
- </interceptors>
- <action name="album!*" class="com.easyjob.cnhuike.actions.AlbumAction" method="{1}">
- <interceptor-ref name="albumUpload" />
- <result>/album/album_{1}.jsp</result>
- <result name="list" type="redirect-action">album!{1}List</result>
- <result name="error">/album/album_error.jsp</result>
- <result name="input">/album/album_error.jsp</result>
- <result name="next" type="redirect-action">
- <param name="actionName">${nextAction}</param>
- <param name="namespace">/album</param>
- </result>
- </action>