文件的上传

来源:互联网 发布:来自mac的照片怎么删除 编辑:程序博客网 时间:2024/06/14 10:01
@RequestMapping(value = "/api/uploadfile", method = RequestMethod.POST)
public void uploadfile(@RequestParam MultipartFile[] file,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
HttpSession session=request.getSession();
ProgressEntity status = (ProgressEntity) session.getAttribute("status");
String filename = null;
PrintWriter outLog = null;
String paths = java.net.URLDecoder.decode(request.getParameter("path"),"UTF-8");
if(paths.substring(paths.length() - 4, paths.length()).equals(".ipa")) {
paths = paths.replace(" ", "_");
}
String[] listpath = paths.split(" ");
int i = 0;
String rootPath = this.getClass().getClassLoader().getResource("/")
.getPath();
rootPath = rootPath.replace("/WEB-INF/classes/", "")+"/";
for (MultipartFile myfile : file) {
filename = myfile.getOriginalFilename();
logger.info("文件原名: " + filename);
int pos = listpath[i].lastIndexOf("/");
String dirs = "";
String localfilename = "";
if(pos > 0){
dirs = listpath[i].substring(0, pos+1);
(new File(rootPath+dirs)).mkdirs();
localfilename = listpath[i].substring(pos+1, listpath[i].length());
}
else {
localfilename = listpath[i];
}
File localfile = new File(rootPath+listpath[i]);
while(localfile.exists()){
localfilename = "_"+localfilename;
listpath[i] = dirs+localfilename;
localfile = new File(rootPath+listpath[i]);
}
logger.info("文件路径: " + localfile);
// logger.info("txtFile is : " + localfile);
FileOutputStream os = new FileOutputStream(localfile);
// logger.info("os is : " + os);
InputStream is = myfile.getInputStream();
// logger.info("is is : " + is);
byte[] buffer = new byte[2048 * 1024];
int length = 0;
long pContentLength = myfile.getSize();
long pBytesRead = 0;
while ((length = is.read(buffer)) > 0) {
pBytesRead += length;
status.setStatus((int) ((( pBytesRead + 0.0)/ pContentLength) * 20) + 80);
os.write(buffer, 0, length);
}
os.close();
is.close();
i++;
}
Map<String,Object> map = new HashMap<String,Object>();
map.put("filepaths", listpath);
outLog = response.getWriter();
outLog.print(new Gson().toJson(map));
outLog.flush();
outLog.close();

}

---------------------------------------------------------------jsp----------------------------------------------------------

function formSubmit(id, apk) {
filepath = "";
var options = {
success : function(files) {
$.ajax({
url : "${mdmweb}/getapkinfos?path="
+ encodeURI(encodeURI(files.filepaths[0])),
type : "POST",
dataType : "text json",
success : function(data) {
//    console.log("data is: " + JSON.stringify(data));
$("#success")
.text(language["appUploadSuccess"]);
filepath = files.filepaths[0];
$("#appName").text(data.newApk.appName);
$("#versionName").text(
data.newApkInfo.versionName);
/* if(typeof(id)=='number')changeHTML(data.newApk, data.newApkInfo.versionName,name,id,apk); 
else apkstatus = true;*/


},
error : function(XMLHttpRequest, textStatus,
errorThrown) {
//    myAlert(language["apkType"]);
uploadapkerr();
}
});
},
dataType : "text json",
};
filepath = "resources/apkStore/tmp/"
+ new Date().Format("yyyy_MM_dd") + "/"
+ realfilename($("#apkURL").val());
uploadfileformSubmit(options, "formSubmit", filepath);


}
//
}

-----------------------------------------------------------------js--------------------------------------------------------------------------------------------------

var isCloseWindows = true;
function uploadfileformSubmit(options, formid, filepath) {
popwin();
isCloseWindows = false;
window.setTimeout(function(){uploadfilecallback(0);}, 30);// 每隔100毫秒执行callback
if(options==null)options = {};
options.url = ('https:' == document.location.protocol ? "https://"
: "http://")
+ window.location.host
+ rootContext + "/api/uploadfile?path="
+ encodeURI(encodeURI(filepath));
success = options.success;
options.success = function(data) {
// console.log("data is: " + JSON.stringify(data));
if(!isCloseWindows){
isCloseWindows = true;
$("#updateBar1").css("width", "100%");
$("#progress_value").html("100%");
window.setTimeout(function(){CloseWindow(10000);}, 1000);
}
if(success != undefined)success(data);
};
$("#" + formid).ajaxSubmit(options);
}

原创粉丝点击