<pre name="code" class="html"><span style="font-family: 宋体; font-size: 12pt;">controller文件:</span>
import com.geego.result.base.ResultMessage;
import com.geego.util.BaseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
@RequestMapping("/file")
public class FileUpLoadController {
private static Logger logger = LoggerFactory.getLogger(FileUpLoadController.class);
private static String filePath = "D:/IdeaProjects/images/";
@ResponseBody
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public Object imageUpload(@RequestParam(value="file")MultipartFile file) {
String path = "";
if (!file.isEmpty()){
if ("image/jpeg".equals(file.getContentType())){
path = saveImgFile(file);
}
}
ResultMessage message = new ResultMessage();
message.addMeta("img_id",path);
return message;
}
@RequestMapping(value = "/upload",method = RequestMethod.OPTIONS)
public void uploadGet(HttpServletResponse response) {
response.setHeader("access-control-allow-headers","accept, content-type");
response.setHeader("access-control-allow-methods","GET, POST, PUT, OPTIONS");
}
@RequestMapping(value = "/img",method = RequestMethod.GET)
public void getImg(HttpServletRequest request,HttpServletResponse response, @RequestParam String img_id){
String path = filePath + img_id;
FileInputStream fis = null;
response.setContentType("image/jpeg");
try {
OutputStream out = response.getOutputStream();
File file = new File(path);
fis = new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);
out.write(b);
out.flush();
} catch (Exception e) {
logger.error("file not found:",e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
logger.error("file read error:",e);
}
}
}
}
public String saveImgFile(MultipartFile file){
try {
logger.debug("upload img");
String fileType = file.getOriginalFilename();
fileType = fileType.substring(fileType.indexOf("."),fileType.length());
String fileName = getFileName();
String path = filePath + fileName + fileType;
// 转存文件
file.transferTo(new File(path));
return fileName + fileType;
} catch (IOException e) {
logger.error("upload img error:",e);
}
return null;
}
public String getFileName(){
String fileName = String.valueOf(BaseUtil.getAtomicCounter());
return fileName;
}
web.xml中加入支持option方法:
因为图片上传实际上是两个请求,第一次是option请求,第一次需要设置header,第二次才是图片上传。所以要实现两个接口<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>dispatchOptionsRequest</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>