前言: 由於項目api是一台服務器,upload也是一台服務器,所以整體思路就是
1,先將zip上傳到upload服務器
2,在upload服務器上解壓zip。
3,在upload服務器上處理解壓的文件(基本上都是json string)
4,通過接口調用將json string傳入到接口中,進行數據庫的操作
一、html
<input id="discoverImg" type="file" name="" class="layui-input">
<input type='button' class="layui-btn layui-btn-primary" onclick="getImg()" value='生成圖片'></input>
二、JS
function getImg(){
var zipUpdate = $("#zipId")[0].files[0];
var formData = new FormData();
formData.append("zipFile",zipUpdate);
uploadZip(formData,callback);
}
function uploadZip(formData, callback){
$.ajax({
url : '/uploadZip',
method : 'POST',
async: false,
processData: false,
contentType:false,
data : formData,
success : function(result) {
callback(result)
}
})
}
function callback(result){
if(result.resultCode==1){
var emojiName = $(".emojiName").val();
// var emojiPath = $(".emojiPath").val();
var emojiProfile = $(".emojiProfile").val();
var emojiEveryName = $(".emojiEveryName").val();
var emojiPath = JSON.stringify(result.data);//將data轉化成json String
Common.invoke({
path : '/add',
data : {
'zipName':emojiName,
'zipPath':emojiPath,//json String
'zipProfile': emojiProfile,
'nameStr' :emojiEveryName
},
successMsg : "",
errorMsg : "",
successCb : function(result) {
}
},
errorCb : function(result) {
}
});
}
}
三、Java、
//一下代碼主要是組裝訪問upload的request和解析結果
public static Map getUploadZipPath(String domain,MultipartFile multipartFile){
String newUrl=null;
Map mapImags = new HashMap();
try {
// 換行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定義數據分隔線
String BOUNDARY = "========7d4a6d158c9";
// 服務器的域名
URL url = new URL(domain+"/upload/UploadZipServlet");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 設置為POST情
conn.setRequestMethod("POST");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 設置請求頭參數
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 上傳文件
String fileRealName = multipartFile.getOriginalFilename();//獲得原始文件名;
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件參數,photo參數名可以隨意修改
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + fileRealName + "\"" + newLine);
sb.append("Content-Type:application/octet-stream");
// 參數頭設置完以后需要兩個換行,然后才是參數內容
sb.append(newLine);
sb.append(newLine);
// 將參數頭的數據寫入到輸出流中
out.write(sb.toString().getBytes());
// 數據輸入流,用於讀取文件數據
DataInputStream in = new DataInputStream(multipartFile.getInputStream());
byte[] bufferOut = new byte[1024];
int bytes = 0;
// 每次讀1KB數據,並且將文件數據寫入到輸出流中
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
// 最后添加換行
out.write(newLine.getBytes());
in.close();
// 定義最后數據分隔線,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine).getBytes();
// 寫上結尾標識
out.write(end_data);
out.flush();
out.close();
// 定義BufferedReader輸入流來讀取URL的響應
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
JSONObject resultObj = JSON.parseObject(reader.readLine());
JSONObject resultData= resultObj.getJSONObject("data");
if(null == resultData)
throw new ServiceException("源文件不存在");
JSONArray imagesData = resultData.getJSONArray("images");
JSONArray imagesDataOthers = resultData.getJSONArray("zip");
List zipImags = Lists.newArrayList();
List zip = Lists.newArrayList();
if (imagesData.size() > 0){
for(int i = 0; i< imagesData.size(); i++){
JSONObject imageData = imagesData.getJSONObject(i);
String newUrlO = imageData.getString("oUrl");
String newUrlT = imageData.getString("tUrl");
System.out.println(" upload new Url =====>"+newUrlO);
System.out.println(" upload new Url =====>"+newUrlT);
newUrl = newUrlO + "," + newUrlT;
zipImags.add(newUrl);
}
mapImags.put("imags",zipImags);
}
if(imagesDataOthers.size()>0){
JSONObject imageDataOthers = imagesDataOthers.getJSONObject(0);
String newUrlOthers = imageDataOthers.getString("oUrl");
zip.add(newUrlOthers);
mapImags.put("zip",zip);
}
} catch (Exception e) {
System.out.println("發送POST請求出現異常!" + e);
e.printStackTrace();
}
return mapImags;
}
//以下主要是一個uploadZip的servlet,我傳的是一些圖片的zip壓縮,而且圖圖片有壓縮的也有原圖。主要的步驟就是在下面,精華。。。。
@WebServlet("/upload/UploadZipServlet")
public class UploadZipServlet extends BaseServlet {
private static final long serialVersionUID = 1L;
public UploadZipServlet() {
super();
}
@Override
protected JMessage hander(HttpServletRequest request, HttpServletResponse response) {
long start = System.currentTimeMillis();
DiskFileItemFactory factory = new DiskFileItemFactory(1000 * 1024 * 1024, new File(getSystemConfig().getuTemp()));
ServletFileUpload fileUpload = new ServletFileUpload(factory);
List<FileItem> multipart = null;
JMessage jMessage = null;
int totalCount = 0;
long userId = 0;
double validTime = 0;
try {
multipart = fileUpload.parseRequest(request);
for (FileItem item : multipart) {
if (item.isFormField()) {
if ("validTime".equals(item.getFieldName())) {
try {
validTime = Double.valueOf(item.getString());
} catch (NumberFormatException e) {
validTime = new Double(-1);
}
}
if ("userId".equals(item.getFieldName())) {
userId = Long.parseLong(item.getString());
}
} else {
if (item.getSize() > 0) {
totalCount++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (null == multipart) {
jMessage = new JMessage(1020101, "表單解析失敗");
}
else if (0 == totalCount) {
jMessage = new JMessage(1010101, "缺少上傳文件");
}
if (null != jMessage)
return jMessage;
jMessage = defHander(multipart, userId, validTime);
int successCount = jMessage.getIntValue("success");
jMessage.put("total", totalCount);
jMessage.put("failure", totalCount - successCount);
jMessage.put("time", System.currentTimeMillis() - start);
return jMessage;
}
protected JMessage defHander(List<FileItem> multipart, long userId, double validTime) {
JMessage jMessage = null;
int successCount = 0;
String oUrl = null;
String tUrl = null;
List<UploadItem> images = Lists.newArrayList();
List<UploadItem> audios = Lists.newArrayList();
List<UploadItem> videos = Lists.newArrayList();
List<UploadItem> others = Lists.newArrayList();
for (FileItem item : multipart) {
UploadItem uploadItem;
if (item.isFormField() || item.getSize() < 1)
continue;
String oFileName = item.getName();
String formatName = ConfigUtils.getFormatName(oFileName);
String newFileName = ConfigUtils.getName(oFileName);
String fileName;
if (!StringUtils.isEmpty(newFileName) && !newFileName.equals(oFileName)) {
fileName = 32 == newFileName.length() ? oFileName : Joiner.on("").join(UUID.randomUUID().toString().replace("-", ""), ".", formatName);
} else {
fileName = oFileName;
}
/*String fileName = 32 == ConfigUtils.getName(oFileName).length() ? oFileName : Joiner.on("").join(UUID.randomUUID().toString().replace("-", ""), ".", formatName);*/
FileType fileType = getFileType(formatName);
File[] uploadPath = ConfigUtils.getUploadPath(userId, fileType);
File oFile = new File(uploadPath[0], fileName);
File tFile = new File(uploadPath[1], fileName);
try {
FileUtils.transfer(item.getInputStream(), oFile);
successCount++;
oUrl = getUrl(oFile);
if(ConfigUtils.getSystemConfig().isOsStatus()){
if (oUrl.contains(ConfigUtils.getSystemConfig().getDomain())){
String urlNewName = oUrl.replace(ConfigUtils.getSystemConfig().getDomain(),"");
String oUrlNew = FileOSUtil.uploadFile(oFile,urlNewName);
log("UploadServlet uploadEd OBS Other oUrl"+oUrlNew);
if (oUrlNew != null){
oUrl = oUrlNew;
}
}
}
uploadItem = new UploadItem(oFileName, oUrl,(byte) 1, null);
ResourcesDBUtils.saveFileUrl(1, oUrl, -1);
others.add(uploadItem);
log("UploadServlet uploadEd " + oUrl);
try {
ZipInputStream ZinO=new ZipInputStream(new FileInputStream(oFile.getPath()));
BufferedInputStream BinO=new BufferedInputStream(ZinO);
String ParentO=uploadPath[0].toString(); //輸出路徑(文件夾目錄)
File fout=null;
ZipEntry entryO;
try {
while ((entryO = ZinO.getNextEntry()) != null && !entryO.isDirectory()) {
fout = new File(ParentO, entryO.getName());
if (!fout.exists()) {
(new File(fout.getParent())).mkdirs();
}
FileOutputStream outO = new FileOutputStream(fout);
BufferedOutputStream BoutO = new BufferedOutputStream(outO);
int b;
while ((b = BinO.read()) != -1) {
BoutO.write(b);
}
BoutO.close();
outO.close();
System.out.println(fout + "解壓成功");
FileInputStream fileInputStream = new FileInputStream(fout);
MultipartFile multipartFile = new MockMultipartFile(fout.getName(), fout.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
images = uploadImg(images,multipartFile,userId,validTime);
}
BinO.close();
ZinO.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
uploadItem = new UploadItem(oFileName, null, (byte) 0, e.getMessage());
others.add(uploadItem);
}
}
Map<String, Object> data = new HashMap<String, Object>();
data.put("zip", others);
data.put("images", images);
jMessage = new JMessage(1, null, data);
jMessage.put("success", successCount);
return jMessage;
}
protected List<UploadItem> uploadImg(List<UploadItem> images,MultipartFile file, long userId, double validTime) {
String oUrl = null;
String tUrl = null;
String oFileName = file.getName();
String formatName = ConfigUtils.getFormatName(oFileName);
String newFileName = ConfigUtils.getName(oFileName);
String imgName;
if (!StringUtils.isEmpty(newFileName) && !newFileName.equals(oFileName)) {
imgName = 32 == newFileName.length() ? oFileName : Joiner.on("").join(UUID.randomUUID().toString().replace("-", ""), ".", formatName);
} else {
imgName = oFileName;
}
FileType imgType = getFileType(formatName);
File[] uploadImgPath = ConfigUtils.getUploadPath(userId, imgType);
File oFile = new File(uploadImgPath[0], imgName);
File tFile = new File(uploadImgPath[1], imgName);
UploadItem uploadItem;
try {
FileUtils.transfer(file.getInputStream(), oFile, tFile, formatName);
oUrl = getUrl(oFile);
tUrl = getUrl(tFile);
if(ConfigUtils.getSystemConfig().isOsStatus()){
if (oUrl.contains(ConfigUtils.getSystemConfig().getDomain())){
String urlNewName = oUrl.replace(ConfigUtils.getSystemConfig().getDomain(),"");
String oUrlNew = FileOSUtil.uploadFile(oFile,urlNewName);
log("UploadServlet uploadEd OBS oUrl "+oUrlNew);
if (oUrlNew != null){
oUrl = oUrlNew;
}
}
if (tUrl.contains(ConfigUtils.getSystemConfig().getDomain())){
String urlNewName = tUrl.replace(ConfigUtils.getSystemConfig().getDomain(),"");
String urlNew = FileOSUtil.uploadFile(tFile,urlNewName);
log("UploadServlet uploadEd OBS tUrl "+urlNew);
if (urlNew != null){
tUrl = urlNew;
}
}
}
ResourcesDBUtils.saveFileUrl(1, oUrl, validTime);
ResourcesDBUtils.saveFileUrl(1, tUrl, validTime);
log("UploadServlet uploadEd " + oUrl);
log("UploadServlet uploadEd " + tUrl);
uploadItem = new UploadItem(oFileName, oUrl, tUrl, (byte) 1, null);
} catch (Exception e) {
e.printStackTrace();
uploadItem = new UploadItem(oFileName, null, (byte) 0, e.getMessage());
}
images.add(uploadItem);
return images;
}
}
還有一個類,就是進行數據庫的處理的接口
首先將json string轉化成json object,之后自己就可以解析啦。。。
JSONObject json = JSONObject.parseObject(zipPath);
Map map = (Map)json;
進來售后說會出現有的zip可以上傳有的不可以,就查了一下問題,報錯是java.lang.IllegalArgumentException: MALFORMED
所以查了一下,說是編碼的問題導致解壓不成功。
參考一個博客 https://blog.csdn.net/guying4875/article/details/81034022
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.BitSet;
/**
* @author 自動識別文件編碼格式
*
*/
public class EncodeUtil {
private static Logger logger = LoggerFactory.getLogger(EncodeUtil.class);
private static int BYTE_SIZE = 8;
public static String CODE_UTF8 = "UTF-8";
public static String CODE_UTF8_BOM = "UTF-8_BOM";
public static String CODE_GBK = "GBK";
/**
* 通過文件全名稱獲取編碼集名稱
*
* @param fullFileName
* @param ignoreBom
* @return
* @throws Exception
*/
public static String getEncode(String fullFileName, boolean ignoreBom) throws Exception {
logger.debug("fullFileName ; {}", fullFileName);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fullFileName));
return getEncode(bis, ignoreBom);
}
/**
* 通過文件緩存流獲取編碼集名稱,文件流必須為未曾
*
* @param bis
* @param ignoreBom 是否忽略utf-8 bom
* @return
* @throws Exception
*/
public static String getEncode(BufferedInputStream bis, boolean ignoreBom) throws Exception {
bis.mark(0);
String encodeType = "未識別";
byte[] head = new byte[3];
bis.read(head);
if (head[0] == -1 && head[1] == -2) {
encodeType = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
encodeType = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) { //帶BOM
if (ignoreBom) {
encodeType = CODE_UTF8;
} else {
encodeType = CODE_UTF8_BOM;
}
} else if ("Unicode".equals(encodeType)) {
encodeType = "UTF-16";
} else if (isUTF8(bis)) {
encodeType = CODE_UTF8;
} else {
encodeType = CODE_GBK;
}
logger.info("result encode type : " + encodeType);
return encodeType;
}
/**
* 是否是無BOM的UTF8格式,不判斷常規場景,只區分無BOM UTF8和GBK
*
* @param bis
* @return
*/
private static boolean isUTF8( BufferedInputStream bis) throws Exception {
bis.reset();
//讀取第一個字節
int code = bis.read();
do {
BitSet bitSet = convert2BitSet(code);
//判斷是否為單字節
if (bitSet.get(0)) {//多字節時,再讀取N個字節
if (!checkMultiByte(bis, bitSet)) {//未檢測通過,直接返回
return false;
}
} else {
//單字節時什么都不用做,再次讀取字節
}
code = bis.read();
} while (code != -1);
return true;
}
/**
* 檢測多字節,判斷是否為utf8,已經讀取了一個字節
*
* @param bis
* @param bitSet
* @return
*/
private static boolean checkMultiByte(BufferedInputStream bis, BitSet bitSet) throws Exception {
int count = getCountOfSequential(bitSet);
byte[] bytes = new byte[count - 1];//已經讀取了一個字節,不能再讀取
bis.read(bytes);
for (byte b : bytes) {
if (!checkUtf8Byte(b)) {
return false;
}
}
return true;
}
/**
* 檢測單字節,判斷是否為utf8
*
* @param b
* @return
*/
private static boolean checkUtf8Byte(byte b) throws Exception {
BitSet bitSet = convert2BitSet(b);
return bitSet.get(0) && !bitSet.get(1);
}
/**
* 檢測bitSet中從開始有多少個連續的1
*
* @param bitSet
* @return
*/
private static int getCountOfSequential( BitSet bitSet) {
int count = 0;
for (int i = 0; i < BYTE_SIZE; i++) {
if (bitSet.get(i)) {
count++;
} else {
break;
}
}
return count;
}
/**
* 將整形轉為BitSet
*
* @param code
* @return
*/
private static BitSet convert2BitSet(int code) {
BitSet bitSet = new BitSet(BYTE_SIZE);
for (int i = 0; i < BYTE_SIZE; i++) {
int tmp3 = code >> (BYTE_SIZE - i - 1);
int tmp2 = 0x1 & tmp3;
if (tmp2 == 1) {
bitSet.set(i);
}
}
return bitSet;
}
/**
* 將一指定編碼的文件轉換為另一編碼的文件
*
* @param oldFullFileName
* @param oldCharsetName
* @param newFullFileName
* @param newCharsetName
*/
public static void convert(String oldFullFileName, String oldCharsetName, String newFullFileName, String newCharsetName) throws Exception {
logger.info("the old file name is : {}, The oldCharsetName is : {}", oldFullFileName, oldCharsetName);
logger.info("the new file name is : {}, The newCharsetName is : {}", newFullFileName, newCharsetName);
StringBuffer content = new StringBuffer();
BufferedReader bin = new BufferedReader(new InputStreamReader(new FileInputStream(oldFullFileName), oldCharsetName));
String line;
while ((line = bin.readLine()) != null) {
content.append(line);
content.append(System.getProperty("line.separator"));
}
newFullFileName = newFullFileName.replace("\\", "/");
File dir = new File(newFullFileName.substring(0, newFullFileName.lastIndexOf("/")));
if (!dir.exists()) {
dir.mkdirs();
}
Writer out = new OutputStreamWriter(new FileOutputStream(newFullFileName), newCharsetName);
out.write(content.toString());
}
}
String fileEncode = EncodeUtil.getEncode(oFile.getPath(),false);
ZipInputStream ZinO=new ZipInputStream(new FileInputStream(oFile.getPath()), Charset.forName(fileEncode));
最后解決。