标签:cat work dia sts erro 文件 技术 ica class
1 package com.csf.weui.service; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.http.HttpServletRequest; 11 import javax.ws.rs.Consumes; 12 import javax.ws.rs.POST; 13 import javax.ws.rs.Path; 14 import javax.ws.rs.Produces; 15 import javax.ws.rs.core.Context; 16 import javax.ws.rs.core.MediaType; 17 import javax.ws.rs.core.MultivaluedMap; 18 19 import org.apache.log4j.Logger; 20 import org.apache.poi.util.IOUtils; 21 import org.jboss.resteasy.plugins.providers.multipart.InputPart; 22 import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; 23 import org.springframework.stereotype.Controller; 24 25 import com.aug3.sys.rs.response.RespType; 26 import com.csf.weui.common.mongo.BaseServiceImpl; 27 import com.csf.weui.repository.UploadRepository; 28 import com.google.common.collect.Maps; 29 30 /** 31 * 上传 32 * 33 * @author fenglei.ma 2017/11/21 11:33 34 */ 35 @Controller 36 @Path("/excel") 37 @Produces("application/json") 38 public class UploadService extends BaseServiceImpl { 39 40 private static final Logger logger = Logger.getLogger(UploadService.class); 41 private final String UPLOADED_FILE_PATH = "C:\\Users\\fenglei.ma\\Desktop\\上传\\上传测试文件\\"; 42 43 private UploadRepository uploadRepository; 44 45 46 /** 47 * <!-- 上传 --> 48 * <dependency> 49 * <groupId>org.jboss.resteasy</groupId> 50 * <artifactId>resteasy-multipart-provider</artifactId> 51 * <version>3.0.9.Final</version> 52 * </dependency> 53 * 54 * @param request 55 * @param file 56 * @return 57 */ 58 @POST 59 @Path("/upload/entity") 60 @Consumes(MediaType.MULTIPART_FORM_DATA) 61 public String upLoadExcel_(@Context HttpServletRequest request, MultipartFormDataInput file) { 62 logger.info("---------->"); 63 try { 64 Map<String, List<InputPart>> uploadForm = file.getFormDataMap(); 65 // <input type="file" name="file_upload" size="50"> 66 List<InputPart> inputParts = uploadForm.get("file_upload"); 67 String fileName = ""; 68 for (InputPart inputPart : inputParts) { 69 MultivaluedMap<String, String> header = inputPart.getHeaders(); 70 fileName = getFileName(header); 71 72 // convert the uploaded file to inputstream 73 InputStream inputStream = inputPart.getBody(InputStream.class, null); 74 byte[] bytes = IOUtils.toByteArray(inputStream); 75 76 // constructs upload file path 77 fileName = UPLOADED_FILE_PATH + fileName; 78 writeFile(bytes, fileName); 79 } 80 return returnSuccAsJson(fileName); 81 } catch (Exception e) { 82 log.error(e, e); 83 return returnParamErrAsJson(RespType.RUNTIME_EXCEPTION.getCode(), "error"); 84 } 85 } 86 87 private String getFileName(MultivaluedMap<String, String> header) { 88 String[] contentDisposition = header.getFirst("Content-Disposition").split(";"); 89 for (String filename : contentDisposition) { 90 if ((filename.trim().startsWith("filename"))) { 91 String[] name = filename.split("="); 92 String finalFileName = name[1].trim().replaceAll("\"", ""); 93 return finalFileName; 94 } 95 } 96 return "unknown"; 97 } 98 99 // save to somewhere 100 private void writeFile(byte[] content, String filename) throws IOException { 101 File file = new File(filename); 102 if (!file.exists()) { 103 file.createNewFile(); 104 } 105 FileOutputStream fop = new FileOutputStream(file); 106 fop.write(content); 107 fop.flush(); 108 fop.close(); 109 } 110 111 public UploadRepository getUploadRepository() { 112 return uploadRepository; 113 } 114 115 public void setUploadRepository(UploadRepository uploadRepository) { 116 this.uploadRepository = uploadRepository; 117 } 118 119 }
上传html
<html>
<body>
<h2>欢迎使用上传!</h2>
<form action="http://localhost:8189/csf-webai/excel/upload/entity" method="post" enctype="multipart/form-data">
请选择要上传的文件<input type="file" name="file_upload" size="50">
<input type="submit" value="提交">
</form>
</body>
</html>
标签:cat work dia sts erro 文件 技术 ica class
原文地址:http://www.cnblogs.com/xiaolei2017/p/7878239.html