zoukankan      html  css  js  c++  java
  • springMVC上传图片

    HTML 页面:

    <%--
      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>

    后台Controller:

    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);
        }
    }
  • 相关阅读:
    Window 中的自带方法Base64
    React_Class1
    npm 常用操作
    React__生命周期
    axios 简单方法个人理解
    JavaScript_Window对象
    常见的搜索引擎技巧
    JS_String常见问题Demo
    java调用C++代码
    java虚拟机指令dup的理解
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/6674954.html
Copyright © 2011-2022 走看看