代码如下:
ExcelServerController.java
package com.xgt.controller; import com.xgt.bean.bs.ExcelBean; import com.xgt.common.BaseController; import com.xgt.common.PcsResult; import com.xgt.exception.EnumPcsServiceError; import com.xgt.util.ConstantsUtil; import com.xgt.util.MD5Util; import com.xgt.util.OssUtil; import org.apache.commons.io.IOUtils; import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; import org.springframework.stereotype.Controller; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; /** * Created by Administrator on 2017/8/31. * 上传Excel到服务器 */ @Controller @Path("/excelserver") public class ExcelServerController extends BaseController{ @POST @Path("/uploadExcel") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public PcsResult uploadExcel(MultipartFormDataInput input) { try { Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); ExcelBean excelBean = convertMap(uploadForm); uploadExcelFile(excelBean); } catch (Exception e) { e.printStackTrace(); PcsResult result = new PcsResult(); result.setCode(EnumPcsServiceError.ERROR_OPERATE.getCode()).setMessage(EnumPcsServiceError.ERROR_OPERATE.getDesc()); result.setSuccess(false); return result; } return newResult(true); } private void uploadExcelFile(ExcelBean excelBean) throws FileNotFoundException { if(excelBean.getExcelPath().length>0){ // 上传到图片服务器
//这里是自己的服务器id,密码等关键信息 OssUtil oss=new OssUtil(accessKeyId, accessKeySecret, endpoint,bucketName); //图片 String EXCELFileName = excelBean.getExcelPathName().substring(0, excelBean.getExcelPathName().lastIndexOf(".")).toLowerCase(); EXCELFileName = MD5Util.MD5(EXCELFileName+System.currentTimeMillis()); String EXCELExtName = excelBean.getExcelPathName().substring( excelBean.getExcelPathName().lastIndexOf("."), excelBean.getExcelPathName().length()) .toLowerCase(); oss.putObject(ConstantsUtil.Folder_EXCEL_CHART+ConstantsUtil.FILE_SEPARATOR +EXCELFileName+EXCELExtName, excelBean.getExcelPath()); excelBean.setExcelPathName(ConstantsUtil.Folder_EXCEL_CHART+ConstantsUtil.FILE_SEPARATOR +EXCELFileName+EXCELExtName); } } /** * 数据摘取组装 * @param uploadForm * @return * @throws IOException */ private ExcelBean convertMap(Map<String, List<InputPart>> uploadForm) throws IOException { ExcelBean excelBean=new ExcelBean(); if(uploadForm!=null) {
//普通实体类String属性inputPart.getBodyAsString()
//接收Integer类型,加一个Integer.parseInt(inputPart.getBodyAsString())即可 if (uploadForm.containsKey("excelPathName")) { InputPart inputPart = uploadForm.get("excelPathName").get(0); inputPart.setMediaType(MediaType.TEXT_PLAIN_TYPE); excelBean.setExcelPathName(inputPart.getBodyAsString()); }
//***文件这么做,用输入流接收 if (uploadForm.containsKey("excelPath")) { InputPart inputPart = uploadForm.get("excelPath").get(0); MultivaluedMap<String, String> header = inputPart.getHeaders(); excelBean.setExcelPathName(getFileName(header)); InputStream inputStream = inputPart.getBody(InputStream.class, null); excelBean.setExcelPath(IOUtils.toByteArray(inputStream)); } } return excelBean; } /** * 返回文件名 * @param header * @return */ private String getFileName(MultivaluedMap<String, String> header) { String[] contentDisposition = header.getFirst("Content-Disposition").split(";"); for (String filename : contentDisposition) { if ((filename.trim().startsWith("filename"))) { String[] name = filename.split("="); String finalFileName = name[1].trim().replaceAll(""", ""); return finalFileName; } } return "unknown"; } }
关键是获取到MultipartFormDataInput的map,解析成ExcelBean类型,并注入进去
BaseController.java
package com.xgt.common; import com.xgt.dao.entity.User; import com.xgt.exception.EnumPcsServiceError; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Value; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * Base controller which contains common methods for other controllers. */ public class BaseController { @Value("${default.pageSize}") private int defPageSize; @Value("${default.maxPageSize}") private int defMaxPageSize; @Value("${aliyunOSS.accessKeyId}") protected String accessKeyId; @Value("${aliyunOSS.accessKeySecret}") protected String accessKeySecret; @Value("${aliyunOSS.uploadUrl}") protected String endpoint; @Value("${aliyunOSS.bucketname}") protected String bucketName; @Value("${aliyunOSS.imageAddress}") protected String imageAddress; /** * Generate new PcsResult object, default with "SUCCESS" code. * * @return PcsResult result. */ protected PcsResult newResult() { PcsResult result = new PcsResult(); result.setCode(EnumPcsServiceError.SUCCESS.getCode()).setMessage(EnumPcsServiceError.SUCCESS.getDesc()); return result; } /** * Generate new PcsResult object, default with "SUCCESS" code. * * @return PcsResult result. */ protected PcsResult newResult(Boolean success) { PcsResult result = new PcsResult(); result.setSuccess(success); result.setCode(EnumPcsServiceError.SUCCESS.getCode()).setMessage(EnumPcsServiceError.SUCCESS.getDesc()); return result; } protected PcsResult failedResult(EnumPcsServiceError error) { PcsResult result = new PcsResult(); result.setCode(error.getCode()).setMessage(error.getDesc()); return result; } @SuppressWarnings("unused") protected PcsResult validateFailedResult(Integer errorCode, String description) { PcsResult result = new PcsResult(); result.setCode(errorCode).setMessage(description); return result; } @SuppressWarnings("unused") protected PcsResult validateFailedResult(EnumPcsServiceError error) { PcsResult result = new PcsResult(); result.setCode(error.getCode()).setMessage(error.getDesc()); return result; } protected Integer getLoginUserId(){ User user = (User) SecurityUtils.getSubject().getPrincipal(); return user.getUserId(); } protected List<Integer> getDepartmentUserIdList(){ User user = (User) SecurityUtils.getSubject().getPrincipal(); return user.getDepartmentUserIdList(); } /** * 构建 分页 页数 * @param page 页 * @return int */ protected int getCurPage(Integer page) { if (page==null || page<1) { return 1; } return page; } /** * 构建 分页 每页显示条数 * @param pageSize 每页显示条数 * @return int */ protected int getPageSize(Integer pageSize) { if (pageSize==null || pageSize<1) { return defPageSize; } if (pageSize>defMaxPageSize) { return defMaxPageSize; } return pageSize; } }
工具类,少文件了可以留言
ConstantsUtil.java
package com.xgt.util; /** * @author CC * 常量工具类 * Created by CC on 2017/2/26. */ public class ConstantsUtil { public final static String FILE_SEPARATOR = "/"; public final static String CONDITIONSPLIT = "\."; //订单大图 public final static String Folder_MAX_IMAGE="Order/Max"; //订单小图 public final static String Folder_MIN_IMAGE="Order/Min"; //人员身份证 public final static String Folder_IDCARD_IMAGE="Personnel/ID_Card"; //人员入职登记表 public final static String Folder_ENTRYFORM_IMAGE="Personnel/EntryForm"; //Excel路径 public final static String Folder_EXCEL_CHART="BaiSheng/Excel"; /** * 订单相关附件类型常量 */ public final static Integer TYPE_MIN_IMAGE=1; public final static Integer TYPE_MAX_IMAGE=2; }
OssUtil.java
package com.xgt.util; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import com.aliyun.oss.OSSException; import org.apache.log4j.Logger; import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.CompleteMultipartUploadRequest; import com.aliyun.oss.model.CompleteMultipartUploadResult; import com.aliyun.oss.model.InitiateMultipartUploadRequest; import com.aliyun.oss.model.InitiateMultipartUploadResult; import com.aliyun.oss.model.ListObjectsRequest; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.OSSObjectSummary; import com.aliyun.oss.model.ObjectListing; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.PartETag; import com.aliyun.oss.model.PutObjectResult; import com.aliyun.oss.model.UploadPartRequest; import com.aliyun.oss.model.UploadPartResult; import org.springframework.beans.factory.annotation.Value; public class OssUtil { private OSSClient client; private String accessKeyId; private String accessKeySecret; private String endpoint; private String bucketName; private Logger _logger = Logger.getLogger(OssUtil.class); private Map<String, String> suffixMap = new HashMap<String, String>(); // 后缀名map public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public String getAccessKeySecret() { return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public String getEndpoint() { return endpoint; } public void setEndpoint(String endpoint) { this.endpoint = endpoint; } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public OssUtil(String accessKeyId, String accessKeySecret, String endpoint,String bucketName) { this.accessKeyId=accessKeyId; this.accessKeySecret=accessKeySecret; this.endpoint=endpoint; this.bucketName=bucketName; // 初始化一个OSSClient this.client = new OSSClient(endpoint, accessKeyId, accessKeySecret); suffixMap.put(".jpg", "image/jpg"); suffixMap.put(".gif", "image/gif"); suffixMap.put(".png", "image/png"); } public static void main(String[] args) { /* ServletContext sc=null; ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); ModelManageService mms = (ModelManageService) ac.getBean("modelManageService"); OssUtils oss = new OssUtils(sc); String accessKeyId = "gJzNJt7MhfihXs59"; String accessKeySecret = "jhGOCGbdzMIq7COPdSEITRcTdC7Z2n"; // 以杭州为例 String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 初始化一个OSSClient oss.client = new OSSClient(endpoint, accessKeyId, accessKeySecret); oss.bucketName="ypxgt-1"; try { // oss.putObject("cc581", "images/aaaa/bbbb/ccc/logo.png", // "D://logo.png"); // oss.listObjects("cc581"); List<YwOrderModel> modelList=mms.getModelList(); for(YwOrderModel model:modelList){ if(oss.isObjectExists(model.getMxlj00()+model.getMxmc00()+model.getMxkzm0())){ mms.updateModelUploadOss(model); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } /** * 从这里去判断是通过普通上传还是分块上传 * * @throws IOException */ public void uploadObject(String key, File file) throws IOException { // 大于50M if (file.length() / (1024 * 1024) >= 50) { this.MultipartUpload(key, file); } else { this.putObject(key, file); } } /** * 简单上传文件 * * @param bucketName * 桶名 * @param key * 文件名 * @param filePath * 文件路径 * @throws FileNotFoundException */ public void putObject(String key, File file) throws FileNotFoundException { InputStream content = new FileInputStream(file); // 创建上传Object的Metadata ObjectMetadata meta = new ObjectMetadata(); // 必须设置ContentLength meta.setContentLength(file.length()); String suffix = key.substring(key.lastIndexOf(".")); meta.setContentType(suffixMap.get(suffix)); // 上传Object. PutObjectResult result = client.putObject(bucketName, key, content, meta); // 打印ETag _logger.info(file.getName() + "上传成功:" + result.getETag()); client.shutdown(); } /** * 简单上传文件 * @param key * 文件名 * @param file * 字节文件 * @throws FileNotFoundException */ public void putObject(String key,byte[] file) throws FileNotFoundException { // 上传Object. PutObjectResult result = client.putObject(bucketName, key, new ByteArrayInputStream(file)); _logger.info("上传成功:" + result.getETag()); client.shutdown(); } /** * 创建文件夹 * * 桶名 * @param objectName * 文件夹名,必须以"/"结尾 * @throws IOException */ public void createFolder(String objectName) throws IOException { ObjectMetadata objectMeta = new ObjectMetadata(); /* * 这里的size为0,注意OSS本身没有文件夹的概念,这里创建的文件夹本质上是一个size为0的Object,dataStream仍然可以有数据 */ byte[] buffer = new byte[0]; ByteArrayInputStream in = new ByteArrayInputStream(buffer); objectMeta.setContentLength(0); try { client.putObject(bucketName, objectName, in, objectMeta); } finally { in.close(); } } /** * 列出指定桶名里的所有Object * */ public void listObjects() { // 获取指定bucket下的所有Object信息 ObjectListing listing = client.listObjects(bucketName); // 遍历所有Object for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) { System.out.println(objectSummary.getKey()); } } public void listFiles() { // 构造ListObjectsRequest请求 ListObjectsRequest listObjectsRequest = new ListObjectsRequest( bucketName); // List Objects ObjectListing listing = client.listObjects(listObjectsRequest); // 遍历所有Object System.out.println("Objects:"); for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) { System.out.println(objectSummary.getKey()); } // 遍历所有CommonPrefix System.out.println("CommonPrefixs:"); for (String commonPrefix : listing.getCommonPrefixes()) { System.out.println(commonPrefix); } } /** * 删除指定文件 * * @param key */ public void deleteObject(String key) { client.deleteObject(bucketName, key); } public File getObject(String key, String newFileName) throws IOException { _logger.info("-------------------开始调用getObject方法"); _logger.info("key:" + key); _logger.info("newFileName:" + newFileName); // 获取Object,返回结果为OSSObject对象 OSSObject object = client.getObject(bucketName, key); // 获取ObjectMeta ObjectMetadata meta = object.getObjectMetadata(); // 获取Object的输入流 InputStream objectContent = object.getObjectContent(); File file = new File(newFileName); if (!file.exists()) { File dir = new File(newFileName.substring(0, newFileName.lastIndexOf("\"))); if (!dir.exists()) { dir.mkdirs(); } file.createNewFile(); OutputStream out = new FileOutputStream(newFileName); byte[] bytes = new byte[objectContent.available()]; while ((objectContent.read(bytes)) != -1) { out.write(bytes); bytes = new byte[objectContent.available()]; } objectContent.close(); out.close(); // 关闭流 // objectContent.close(); return file; } return file; } public void MultipartUpload(String key, File partFile) throws IOException { // 开始Multipart Upload InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest( bucketName, key); InitiateMultipartUploadResult initiateMultipartUploadResult = client .initiateMultipartUpload(initiateMultipartUploadRequest); // 打印UploadId System.out.println("UploadId: " + initiateMultipartUploadResult.getUploadId()); // 设置每块为 10M final int partSize = 1024 * 1024 * 10; // 计算分块数目 int partCount = (int) (partFile.length() / partSize); if (partFile.length() % partSize != 0) { partCount++; } // 新建一个List保存每个分块上传后的ETag和PartNumber List<PartETag> partETags = new ArrayList<PartETag>(); for (int i = 0; i < partCount; i++) { // 获取文件流 FileInputStream fis = new FileInputStream(partFile); // 跳到每个分块的开头 long skipBytes = partSize * i; fis.skip(skipBytes); // 计算每个分块的大小 long size = partSize < partFile.length() - skipBytes ? partSize : partFile.length() - skipBytes; // 创建UploadPartRequest,上传分块 UploadPartRequest uploadPartRequest = new UploadPartRequest(); uploadPartRequest.setBucketName(bucketName); uploadPartRequest.setKey(key); uploadPartRequest.setUploadId(initiateMultipartUploadResult .getUploadId()); uploadPartRequest.setInputStream(fis); uploadPartRequest.setPartSize(size); uploadPartRequest.setPartNumber(i + 1); UploadPartResult uploadPartResult = client .uploadPart(uploadPartRequest); // 将返回的PartETag保存到List中。 partETags.add(uploadPartResult.getPartETag()); // 关闭文件 fis.close(); } CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest( bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags); // 完成分块上传 CompleteMultipartUploadResult completeMultipartUploadResult = client .completeMultipartUpload(completeMultipartUploadRequest); // 打印Object的ETag System.out.println(completeMultipartUploadResult.getETag()); } /** * 获取dir下所有的文件 * * @param dir * @return */ public List<OSSObjectSummary> getFileLstInDir(String dir) { // 构造ListObjectsRequest请求 ListObjectsRequest listObjectsRequest = new ListObjectsRequest( bucketName); // "/" 为文件夹的分隔符 listObjectsRequest.setDelimiter("/"); // 列出fun目录下的所有文件和文件夹 listObjectsRequest.setPrefix(dir + "/"); ObjectListing listing = client.listObjects(listObjectsRequest); return listing.getObjectSummaries(); } /** * 判断OSS上是否有key为keyName的Object * * @param keyName * @return */ public boolean isObjectExists(String keyName) { // 获取Object,返回结果为OSSObject对象 try { OSSObject object = client.getObject(bucketName, keyName); if (object != null) { return true; } } catch (OSSException e) { return false; } return false; } /** * 从OSS上下载CKT文件至本地并打包上传至OSS上 * * @param wjjPath * @param localPath * @throws Exception */ public void cktZip(String wjjPath, String localPath) throws Exception { _logger.info("wjjPath:" + wjjPath); _logger.info("localPath:" + localPath); // 获取OSS目录下参考图的List List<OSSObjectSummary> fileList = getFileLstInDir(wjjPath); _logger.info(fileList.size()); if (fileList == null || fileList.size() == 0) { return; } File cktDir = new File(localPath + wjjPath); _logger.info(cktDir.getName()); if (!cktDir.exists()) { cktDir.mkdirs(); } for (OSSObjectSummary ossObject : fileList) { String fileName = ossObject.getKey(); _logger.info("fileName:" + fileName); // 将OSS的文件取回本地,为下一步压缩ZIP包作准备 getObject(fileName, localPath + fileName.replaceAll("/", "\\")); } ZipUtils zipUtils = new ZipUtils(); _logger.info("need zip's dirName:" + localPath + wjjPath); zipUtils.compress(localPath + wjjPath); File zipFile = new File(localPath + wjjPath.replaceAll("/", "\\") + ".zip"); _logger.info("压缩后的文件名:" + localPath + wjjPath.replaceAll("/", "\\") + ".zip"); putObject(wjjPath + ".zip", zipFile); if (zipFile.exists()) { zipFile.delete(); } } }
MD5Util.java
package com.xgt.util; import java.security.MessageDigest; import java.util.Random; public class MD5Util { public final static String MD5(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; try { byte[] btInput = s.getBytes(); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; } } public static String toMD5(byte[] source) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(source); StringBuffer buf = new StringBuffer(); for (byte b : md.digest()) buf.append(String.format("%02x", b & 0xff)); return buf.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } public static String getRandomPassword(int pwd_len) { // 35是因为数组是从0开始的,26个字母+10个数字 final int maxNum = 10; int i; // 生成的随机数 int count = 0; // 生成的密码的长度 // char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', // 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', // 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < pwd_len) { // 生成随机数,取绝对值,防止生成负数, i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); } public static void main(String[] args) { } }