spring MVC 上傳圖片 DEMO
阿新 • • 發佈:2019-02-15
HTML 頁面:
後臺Controller<%-- Created by IntelliJ IDEA. User: john Date: 14-9-1 Time: 下午4:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <td width="100" align="right">照片:</td> <td><input type="file" name="studentPhoto"/> <input type="submit"></td> </tr> </table> </form> </body> </html>
package com.gochaintv.copyright.controller; import com.gochaintv.copyright.util.FileUpload; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.OutputStream; /** * Created by shhao. * Date: 14-9-1 * Time:下午4:32 */ @Controller public class FileUploadController { Logger logger = LoggerFactory.getLogger(FileUploadController.class); @RequestMapping("upload") public void upload(@RequestParam("studentPhoto") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { String filePath = FileUpload.uploadFile(file, request); logger.info("filePath:" + filePath); response.setContentType("text/html;charset=utf8"); response.getWriter().write("<img src='"+filePath+"'/>"); } @RequestMapping("download") public void download(String fileName, HttpServletResponse response) throws IOException { OutputStream os = response.getOutputStream(); try { response.reset(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("image/jpeg; charset=utf-8"); os.write(FileUtils.readFileToByteArray(FileUpload.getFile(fileName))); os.flush(); } finally { if (os != null) { os.close(); } } } }
package com.gochaintv.copyright.util; import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.Date; /** * Created by shhao. * Date: 14-9-1 * Time:下午4:12 */ public class FileUpload { public static final String FILE_PATH = "/upload/"; //檔案上傳 public static String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException { String fileName = file.getOriginalFilename(); File tempFile = new File(FILE_PATH, new Date().getTime() + String.valueOf(fileName)); if (!tempFile.getParentFile().exists()) { tempFile.getParentFile().mkdir(); } if (!tempFile.exists()) { tempFile.createNewFile(); } file.transferTo(tempFile); return "/download?fileName=" + tempFile.getName(); } public static File getFile(String fileName) { return new File(FILE_PATH, fileName); } }