SPRINGMVC模式上传文件
这里包括三个上传的方式都是项目当中用到过的,都是大同小异主要还是运用SPRINGMVC里面的 MultipartFile file
package com.xiaoquan.approval.controller.admin;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.xiaoquan.approval.core.BaseController;
import com.xiaoquan.approval.domain.T_ProjectFile;
import com.xiaoquan.approval.domain.T_ProjectIdea;
import com.xiaoquan.approval.domain.T_Users;
import com.xiaoquan.approval.modules.admin.service.T_ProjectFileService;
import com.xiaoquan.approval.modules.admin.service.T_ProjectIdeaService;
import com.xiaoquan.approval.modules.admin.service.T_UsersService;
import com.xiaoquan.approval.utils.EncryptAlgorithm;
import com.xiaoquan.approval.utils.SecurityContextUtil;
@Controller
@RequestMapping(value = "/admin/file/*")
public class AdminFileUploadController extends BaseController {
@Autowired private T_ProjectFileService filesService;
@Autowired private T_UsersService t_UsersService;
@Autowired private T_ProjectIdeaService t_ProjectIdeaService;
//项目文件
@ResponseBody
@RequestMapping(value = "/upload/adjuncted",produces = "text/html;charset=UTF-8",method=RequestMethod.POST)
public String adjunct(HttpServletRequest request,HttpServletResponse response,
@RequestParam("files") MultipartFile[] files,Integer type,String fileName0,Long projectId){
if(files==null||files.length!=1){
return getAjax(1, "请选择一个文件", null);
}
MultipartFile myFile=files[0];
if(myFile.isEmpty()){
return getAjax(1, "请选择一个文件", null);
}
T_ProjectFile tf=new T_ProjectFile();
String realPath=null;
String htmlPath=null;
String tmpPath =this.getClass().getResource("").getPath();
String storagePath= tmpPath.substring(0, tmpPath.lastIndexOf("/WEB"));
try {
DateFormat dateFormat=new SimpleDateFormat("/yyyy/MM/dd/");
String prefix="/adjunct";
if(type==0){
prefix="/adjunct";
}else{
prefix="/files";
}
String upload="/upload";
String dataPath=dateFormat.format(new Date());
File fileDir=new File(storagePath+upload+prefix+dataPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
String fileName = myFile.getOriginalFilename();
//扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
/*if(!fileExt.equals("zip")){
return getFailure("文件上传为压缩包!");
}*/
if(!Arrays.<String>asList("zip,rar,gz,bz2".split(",")).contains(fileExt)){
return getAjax(300, "上传文件扩展名是不允许的扩展名。\n只允许" + "zip,rar,gz,bz2" + "格式", null);
}
//uuid
String uuidName=UUID.randomUUID().toString().replace("-", "");
realPath=storagePath+upload+prefix+dataPath+uuidName+"."+fileExt;
htmlPath=upload+prefix+dataPath+uuidName+"."+fileExt;
File file=new File(realPath);
FileUtils.copyInputStreamToFile(myFile.getInputStream(),file);
tf.setProjectFilePath(htmlPath);
//重命名,换名称
if(null != fileName0 && !"".equals(fileName0)){
tf.setRemark(fileName0);
}else{
return getFailure("名字不能为空!");
}
filesService.saveOrUpdate(tf);
} catch (Exception e) {
e.printStackTrace();
return getFailure("系统错误");
}
return getSuccee("保存成功!", tf);
}
//头像
@ResponseBody
@RequestMapping(value = "/upload/edit", produces = "text/html;charset=UTF-8")
public String picture(Model model,Integer type,String newSrc){
T_Users user = SecurityContextUtil.getSessionUser();
T_Users tf=new T_Users();
if(newSrc==null||("").equals(newSrc)){
return getAjax(1, "请选择一个文件", null);
}
String htmlPath=null;
String upload="/upload";
String prefix="/touxiang";
String tmpPath =this.getClass().getResource("").getPath();
String storagePath= tmpPath.substring(0, tmpPath.lastIndexOf("/WEB"));
try {
DateFormat dateFormat=new SimpleDateFormat("/yyyy/MM/dd/");
String dataPath=dateFormat.format(new Date());
newSrc=newSrc.substring(22);
File file=new File(storagePath+upload+prefix+dataPath+user.getId()+".png");
byte[] a=EncryptAlgorithm.decodeBase64(newSrc);
FileUtils.writeByteArrayToFile(file, a);
htmlPath=upload+prefix+dataPath+user.getId()+".png";
tf.setId(user.getId());
tf.setPath(htmlPath);
t_UsersService.saveOrUpdate(tf);
} catch (Exception e) {
e.printStackTrace();
return getFailure("系统错误");
}
return getSuccee("保存成功!",tf);
}
@ResponseBody
@RequestMapping(value = "/upload/partitionIdea",produces = "text/html;charset=UTF-8",method=RequestMethod.POST)
public String partitionIdea(HttpServletRequest request,HttpServletResponse response,
@RequestParam("files") MultipartFile[] files,Integer type,String fileName0){
if(files==null||files.length!=1){
return getAjax(1, "请选择一个文件", null);
}
MultipartFile myFile=files[0];
if(myFile.isEmpty()){
return getAjax(1, "请选择一个文件", null);
}
T_ProjectIdea tf=new T_ProjectIdea();
String realPath=null;
String htmlPath=null;
String tmpPath =this.getClass().getResource("").getPath();
String storagePath= tmpPath.substring(0, tmpPath.lastIndexOf("/WEB"));
try {
DateFormat dateFormat=new SimpleDateFormat("/yyyy/MM/dd/");
String prefix="/adjunct";
if(type==0){
prefix="/adjunct";
}else{
prefix="/files";
}
String upload="/upload";
String dataPath=dateFormat.format(new Date());
File fileDir=new File(storagePath+upload+prefix+dataPath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
String fileName = myFile.getOriginalFilename();
//扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
//uuid
String uuidName=UUID.randomUUID().toString().replace("-", "");
realPath=storagePath+upload+prefix+dataPath+uuidName+"."+fileExt;
htmlPath=upload+prefix+dataPath+uuidName+"."+fileExt;
File file=new File(realPath);
FileUtils.copyInputStreamToFile(myFile.getInputStream(),file);
//重命名,换名称
if(null != fileName0 && !"".equals(fileName0)){
tf.setPictureName(fileName0);
}else{
return getFailure("名字不能为空!");
}
tf.setPicture(htmlPath);
} catch (Exception e) {
e.printStackTrace();
return getFailure("系统错误");
}
return getSuccee("保存成功!", tf);
}
}