SpringMVC实现angularjs图片上传

<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方法:
 
<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>
因为图片上传实际上是两个请求,第一次是option请求,第一次需要设置header,第二次才是图片上传。所以要实现两个接口


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值