文件上傳

1. 配置上傳頁面web

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

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

      <tr>spa

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

      </tr>圖片

      <tr>ip

        <td>商品圖片:</td>get

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

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

相關文章
相關標籤/搜索