struts利用插件jqueryupload上传文件,后台处理文件代码

package cn.axhu.education.action.file;

import cn.axhu.education.common.FileSystemConfig;
import cn.axhu.education.common.utils.FileUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

/**
 * Created by yi.wang1 on 2015/3/9.
 */
@ParentPackage("default")
@Namespace("/file")
public class ChapterAttachUploadAction extends SimpleActionSupport {

    @Action("saveAttachFile")
    public void saveAttachFile() {
        PrintWriter out = null;
        try {
            //初始化变量
            InputStream stream = null;
            String ret_fileName = null;//返回给前端已修改的文件名称
            Map<String, Object> result = new HashMap<String, Object>();
            String ret_filePath;
            File uploadedFile;
            String uuid_fileName;
            try {
                //设置字符集
                request.setCharacterEncoding("UTF-8");
                response.setContentType("text/html; charset=UTF-8");
                out = response.getWriter();

                // 文件保存目录路径
                String savePath = FileSystemConfig.getFileRootPath() + "chapterAttach/";

                // 创建文件夹
                File dirFile = new File(savePath);
                if (!dirFile.exists()) {
                    dirFile.mkdirs();
                }
                //获取文件流
                MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
                if (multiWrapper.hasErrors()) {
                    for (Iterator errorIter = multiWrapper.getErrors().iterator(); errorIter.hasNext(); ) {
                        String error = (String) errorIter.next();
                        LOG.error(error);
                    }
                }
                File file = null;
                Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
                while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
                    String inputName = (String) fileParameterNames.nextElement();
                    String[] contentType = multiWrapper.getContentTypes(inputName);
                    if (contentType != null) {
                        String[] fileName = multiWrapper.getFileNames(inputName);
                        if (fileName != null) {
                            ret_fileName = fileName[0];
                            File[] files = multiWrapper.getFiles(inputName);
                            file = files[0];
                        }
                    }
                }
                stream = new FileInputStream(file);

                //初始化文件名,并定义上传文件
                UUID uuid = UUID.randomUUID();
                String suffix = ret_fileName.substring(ret_fileName.lastIndexOf(".") + 1);
                uuid_fileName = uuid.toString() + "." + suffix;
                ret_filePath = savePath + "/" + uuid_fileName + ".temp";
                uploadedFile = new File(ret_filePath);
                FileUtils.saveFile(uploadedFile, stream);
            } catch (Exception e) {
                //读写错误异常处理
                result.put("message", e.getMessage());
                out.print(new JSONObject(result).toString());
                return;
            } finally {
                //资源关闭异常处理
                try {
                    if (stream != null) {
                        stream.close();
                    }
                } catch (Exception e) {
                    result.put("message", e.getMessage());
                    out.print(new JSONObject(result).toString());
                    return;
                }
            }
            result.put("message", "success");
            result.put("fileName", ret_fileName);
            result.put("filePath", "chapterAttach/" + uuid_fileName + ".temp");
            out.print(new JSONObject(result).toString());
        } finally {
            //将信息返回
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。