前端:
export default{ data(){ return { fileList: [] } }, methods: { methods:{ submitUpload() { this.$refs.upload.submit(); this.getUpgradeFile(); }, handleRemove(file, fileList) { console.log(file, fileList); }, handlePreview(file) { console.log(file); } }}复制代码 选取文件 上传服务 器
spring.xml配置文件:
复制代码
后端 :
package sino.com.controller;import java.io.Console;import java.io.File;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.multipart.commons.CommonsMultipartResolver;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.jfinal.upload.UploadFile;import sino.com.controller.utils.ResponseMsg;import x.acs.utils.AcsUtil;@Controller@RequestMapping(value="/firewareUpgrade")public class UpgradeFileController extends com.jfinal.core.Controller{ @Resource ResponseMsg responseMsg; @RequestMapping(value="/operationUpgradeFile", method=RequestMethod.POST) public void importUpgradeFile(PrintWriter out, HttpServletRequest request){ String dirPath = AcsUtil.getFirmwarePath(); try { // @RequestParam("file") MultipartFile file, CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); // 判断 request 是否有文件上传,即多部分请求 if (multipartResolver.isMultipart(request)) { // 转换成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 取得request中的所有文件名 Iteratoriter = multiRequest.getFileNames(); while (iter.hasNext()) { // 取得上传文件 MultipartFile f = multiRequest.getFile(iter.next()); if (f != null) { // 取得当前上传文件的文件名称 String myFileName = f.getOriginalFilename(); // 如果名称不为“”,说明该文件存在,否则说明该文件不存在 if (myFileName.trim() != "") { // 定义上传路径 String path = dirPath+"\\" + myFileName; File localFile = new File(path); f.transferTo(localFile); } } } } responseMsg.setStatusCode(ResponseMsg.SUCCESS_CODE); responseMsg.setBody(new String("升级文件导入成功!")); } catch (Exception e) { responseMsg.setStatusCode(ResponseMsg.FAILURE_CODE); responseMsg.setBody(new String("上传的升级文件被删除!")); }finally{ out.println(JSON.toJSONString(responseMsg)); } }}复制代码