jsp:
<div class="form-group">
<label class="col-sm-3 control-label">原代码上传:</label>
<div class="col-sm-8">
<input name="oldCodeName" id="oldCodeName" class="form-control" type="text" style="width:calc(100% - 100px);float: left; margin-right: 3px;">
<button id="uploadBtn" type="button" class="btn btn-sm btn-primary" style="display: inline-block; width: 95px;position: relative;float: left;height: 33px;"><i class="fa fa-check"></i>上 传</button>
<input name="oldCodeUrl" id="oldCodeUrl" class="form-control" type="hidden">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">新代码上传:</label>
<div class="col-sm-8">
<input name="newCodeName" id="newCodeName" class="form-control" type="text" style="width:calc(100% - 100px);float: left; margin-right: 3px;">
<button id="uploadBtnTwo" type="button" class="btn btn-sm btn-primary" style="display: inline-block; width: 95px;position: relative;float: left;height: 33px;"><i class="fa fa-check"></i>上 传</button>
<input name="newCodeUrl" id="newCodeUrl" class="form-control" type="hidden">
</div>
</div>
js:
//触发的id
var ids = new Array("uploadBtn","uploadBtnTwo");
$.each(ids,function(i,n){
var self = this.toString();
var uploader = new plupload.Uploader({ //实例化一个plupload上传对象
runtimes: 'html5,silverlight,html4,flash',
browse_button : self,
url : prefix+"/uploadFroOperation",
flash_swf_url : "/ajax/libs/plupload/Moxie.swf",
silverlight_xap_url : "/ajax/libs/plupload/Moxie.xap",
filters: {
max_file_size: '1gb', //最大上传文件大小(格式100b, 10kb, 10mb, 1gb)
mime_types : [{ title : "图片文件", extensions:"jpg,jpeg,gif,png,bmp" },
{title: "视频文件", extensions: "mp4,mpeg4,3gp,wav,wma,wmv"},
{title: "文本文件", extensions: "txt,pdf,doc,docx,xls,xlsx,ppt,pptx,ini"},
{title: "视频文件", extensions: "zip,rar,gz,bz2,jar"},
{title: "音频文件", extensions: "mp3,mid,wav,wma,avi"}
]
}//只允许上传图片文件
});
uploader.init(); //初始化
//绑定文件添加进队列事件
uploader.bind('FilesAdded',function(uploader,files){
var flag = self.search('1');// -1 表示没找到
uploader.start();
});
uploader.bind('Error',function(up, err){
alert(err.message);//上传出错的时候触发
});
uploader.bind("FileUploaded", function(up, file, res){
if(self.search("uploadBtn")==0 &&self.search("uploadBtnTwo")== -1){
var json=res.response;
json=JSON.parse(json);
if(json.code!=0&&json.code!='0'){
$.modal.alertError(json.msg);
}
uploadCallbackONE(json.data);
}else if(self.search("uploadBtn")==0 &&self.search("uploadBtnTwo")== 0){
var json=res.response;
json=JSON.parse(json);
if(json.code!=0&&json.code!='0'){
$.modal.alertError(json.msg);
}
uploadCallbackTWO(json.data);
}
})
});
function uploadCallbackONE(json){
$("#oldCodeName").val(json.name);
$("#oldCodeUrl").val(json.path);
}
function uploadCallbackTWO(json){
$("#newCodeName").val(json.name);
$("#newCodeUrl").val(json.path);
}
JAVA:
/**
* 上传资源请求
*/
@PostMapping("/uploadFroOperation")
@ResponseBody
public AjaxResult uploadResource(MultipartFile file) throws Exception
{
try
{
// 上传并返回新文件名称
String path = FileUploadUtils.upload(Global.getUploadPath(), file);
Map map=new HashMap();
map.put("path",path);
map.put("size",file.getSize());
map.put("name",file.getOriginalFilename());
return AjaxResult.success(map);
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
/**
* 下载请求
*
* @param fileName 文件名称
*/
@GetMapping("/downloadFroOperation/{id}")
public void fileDownload(@PathVariable("id") Long id, HttpServletResponse response, HttpServletRequest request)
{
try
{
ProductOperation productOperation = productOperationService.selectProductOperationById(id);
String oldCodeUrl=productOperation.getOldCodeUrl();
String oldCodeName=productOperation.getOldCodeName();
if(oldCodeUrl!=null) {
String fileName =oldCodeUrl;
String[] split = fileName.split("/profile");
//fileName= split[1].replaceAll("/","\\\\");
fileName=split[1].toString();
String realFileName = System.currentTimeMillis()+"_"+oldCodeName;
String filePath = Global.getProfile()+fileName;
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
}else {
throw new Exception(StringUtils.format("没有对应文件,不允许下载。 ", oldCodeUrl));
}
}catch (Exception e) {
System.out.println("【错误信息】"+e);
e.printStackTrace();
}
}
/**
* 下载请求
*
* @param fileName 文件名称
*/
@GetMapping("/downloadFroNewOperation/{id}")
public void downloadFroNewOperation(@PathVariable("id") Long id, HttpServletResponse response, HttpServletRequest request)
{
try
{
ProductOperation productOperation = productOperationService.selectProductOperationById(id);
String newCodeUrl=productOperation.getNewCodeUrl();
String newcodeName=productOperation.getNewCodeName();
if(newCodeUrl!=null){
String fileName =newCodeUrl;
String[] split = fileName.split("/profile");
//fileName= split[1].replaceAll("/","\\\\");
fileName=split[1].toString();
String realFileName = System.currentTimeMillis()+"_"+newcodeName;
String filePath = Global.getProfile()+fileName;
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
}else {
throw new Exception(StringUtils.format("没有对应文件,不允许下载。 ", newCodeUrl));
}
}catch (Exception e) {
System.out.println("【错误信息】"+e);
e.printStackTrace();
}
}
该博客介绍了如何在JSP页面中使用Uploader组件实现两个独立的文件上传按钮,分别用于上传原代码和新代码。通过JavaScript代码展示了如何实例化和配置plupload上传对象,并绑定了事件处理函数以处理文件添加、上传错误和文件上传成功后的回调。同时,提供了对应的Java后台处理上传的方法,包括上传资源和下载资源的实现。
214

被折叠的 条评论
为什么被折叠?



