|
|
|
@ -0,0 +1,66 @@
|
|
|
|
|
package com.platform.controller;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
|
|
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.base.BaseController;
|
|
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
@RequestMapping("/fileOperation")
|
|
|
|
|
public class ExcelController extends BaseController{
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/file/upload" , method = RequestMethod.POST)
|
|
|
|
|
public void upload(HttpServletRequest request, HttpServletResponse respone){
|
|
|
|
|
System.out.println(request.getAttribute("file"));
|
|
|
|
|
System.out.println(request.getParameter("file"));
|
|
|
|
|
//解析器解析request的上下文
|
|
|
|
|
CommonsMultipartResolver multipartResolver =
|
|
|
|
|
new CommonsMultipartResolver(request.getSession().getServletContext());
|
|
|
|
|
//先判断request中是否包涵multipart类型的数据,
|
|
|
|
|
if(multipartResolver.isMultipart(request)){
|
|
|
|
|
//再将request中的数据转化成multipart类型的数据
|
|
|
|
|
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
|
|
|
|
|
Iterator iter = multiRequest.getFileNames();
|
|
|
|
|
while(iter.hasNext()){
|
|
|
|
|
//这里的name为fileItem的alias属性值,相当于form表单中name
|
|
|
|
|
String name=(String)iter.next();
|
|
|
|
|
System.out.println("name:"+name);
|
|
|
|
|
//根据name值拿取文件
|
|
|
|
|
MultipartFile file = multiRequest.getFile(name);
|
|
|
|
|
if(file != null){
|
|
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
|
|
String path = "D:/test/" + fileName;
|
|
|
|
|
File localFile = new File(path);
|
|
|
|
|
if(!localFile.getParentFile().exists()) {
|
|
|
|
|
//如果目标文件所在的目录不存在,则创建父目录
|
|
|
|
|
localFile.getParentFile().mkdirs();
|
|
|
|
|
System.out.println("parent:"+localFile.getParentFile().getPath());
|
|
|
|
|
}
|
|
|
|
|
//写文件到本地
|
|
|
|
|
try {
|
|
|
|
|
file.transferTo(localFile);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("-----");
|
|
|
|
|
respone.setStatus(200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|