JAVA-AOSUite文件,图片上传功能
关于jsp页面部分
Markup
关于上传图片js部分
Markup
function uploadPic(){
var record = AOS.selectone(g_dic); if(record){
f_pic_upload.loadRecord(record); }
if(!f_pic_upload.isValid()) {
Ext.Msg.alert(\系统提示\请选择文件后再上传!\ return; }
f_pic_upload.getForm().fileUpload = true; f_pic_upload.getForm().submit({
url:'do.jhtml?router=aosCeshiExtService.savePic&juid=${juid}', waitMsg:'文件上传中...', success: function() {
AOS.tip(\非常OK,上传成功!\ g_dic_store.reload(); w_pic_upload.hide();
},
failure: function() { w_pic_upload.hide();
AOS.tip(\文件上传失败!\ } });
}
关于后台接收部分的,以及转存
Java
/**
* 图片上传及保存 *
* @param httpModel */
@Transactional
publicvoidsavePic(HttpModel httpModel){ Dto inDto = httpModel.getInDto();
UploadFile uploadFile =newUploadFile(); AosCeshiPO aosCeshiPO =newAosCeshiPO(); aosCeshiPO.copyProperties(inDto); try{
Map
aosCeshiPO.setFile_path(map.get(\).toString()); aosCeshiDao.updateByKey(aosCeshiPO); }catch(Exception e){
// TODO Auto-generated catch block e.printStackTrace(); }
String msg =\上传成功!'}\; httpModel.setOutMsg(msg); }
Java
String infoId =\;
public Map
MultipartHttpServletRequest multipartRequest = null; if(request instanceofMultipartHttpServletRequest){ try{
multipartRequest =(MultipartHttpServletRequest) request;
}catch(Exception ex){
ex.getMessage(); } }
if(multipartRequest != null){
Map
String newName =\; String url =\;
String paths =getWebPath(); String newNameSub=\;
for(Map.Entry
//String infoId = request.getParameter(\
newName =getFileName(request,myfile);
newNameSub=newName.substring(newName.length()-3, newName.length());
if(newNameSub.equals(\)||newNameSub.equals(\)||newNameSub.equals(\)){
url = paths +\+newName; }elseif(newNameSub.equals(\)){
url = paths +\+newName; }else{
url = paths +\+newName; }
map.put(\,true); map.put(\, url); } } }
}catch(Exception e){
System.out.println(e.getMessage()); }
return map; }
public String getFileName(HttpServletRequest request,MultipartFile file)throws Exception{ // 转存文件
String
name=file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(\), file.getOriginalFilename().length());//获取文件后缀
String newName = publicFile.createData().toString()+name;
String path = request.getSession().getServletContext().getRealPath(\);//项目存放路径
//截取字符串取后三位并转小写 String
nameSub=name.substring(name.length()-3,name.length()).toLowerCase(); if(nameSub.equals(\)||nameSub.equals(\)||nameSub.equals(\)){ path = path +\; }elseif(nameSub.equals(\)){
path = path +\; }else{
path = path +\; }
File targetFile =newFile(path, newName); if(!targetFile.exists()){
targetFile.mkdirs(); } try{
file.transferTo(targetFile); }catch(IllegalStateException e){ e.printStackTrace(); }catch(IOException e){
e.printStackTrace(); }
return newName; }