zoukankan      html  css  js  c++  java
  • 文件上传

    1. 配置上传页面

      <form action="${pageContext.request.contextPath}/file.action" enctype="multipart/form-data">

        <table align="center" border="1" width="45%">

          <tr>

            <td colspan="2" align="center">文件上传</td>

          </tr>

          <tr>

            <td>商品图片:</td>

            <td><input name="photo" type="file"/></td>

          </tr>

          <tr>

            <td colspan="2" align="center"><input type="submit" value="提交"/></td>

          </tr>

        </table>

    2. 解析上传文件

    @Controller
    public class FileController {
        
        //转向文件长传页面
        @RequestMapping("/toFile.action")
        public String toFile(){
            return "file";
        }
        
        
        @RequestMapping("/file.action")
        public String updloadFile(MultipartFile photo,Model model) throws IOException{
            
            //简单方式
            FileUtils.writeByteArrayToFile
            (new File("D:\图片样例\"+photo.getOriginalFilename()), photo.getBytes());
            
            
            model.addAttribute("msg", "文件上传成功");
            //return "forward:/toFile.action";
            return "redirect:/toFile.action";
        }
    }

    3. 配置文件上传解析器

     <!--配置文件上传解析器  id的名称必须为:multipartResolver-->
           <bean id="multipartResolver"
           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                   <property name="maxUploadSize" value="10485760"></property>
           </bean>

        

  • 相关阅读:
    管道通信
    进程间的八种通信方式----共享内存是最快的 IPC 方式
    归并排序时间复杂度
    vector中的push_back函数的意思是什么
    如何实现android和服务器长连接
    android中实现service动态更新UI界面
    android中如何实现UI的实时更新---需要考虑电量和流量
    Map集合排序
    (二十一)自定义Tabbar
    (二十)首页内容详细【详细页】+ 评论 + 回复
  • 原文地址:https://www.cnblogs.com/gerald-x/p/7107161.html
Copyright © 2011-2022 走看看