1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 怎样将压缩文件上传到服务器上

怎样将压缩文件上传到服务器上

时间:2018-08-24 19:08:41

相关推荐

怎样将压缩文件上传到服务器上

前言:由于项目api是一台服务器,upload也是一台服务器,所以整体思路就是

1,先将zip上传到upload服务器

2,在upload服务器上解压zip。

3,在upload服务器上处理解压的文件(基本上都是jsonstring)

4,通过接口调用将jsonstring传入到接口中,进行数据库的操作

一、html

<input id="discoverImg" type="file" name="" class="layui-input">

<input type='button' class="layui-btn layui-btn-primary" οnclick="getImg()" value='生成图片'></input>

二、JS

functiongetImg(){

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(101, "表单解析失败");

}

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;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。