Java源码文件上传

jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试文件上传</title>

    <script type="text/javascript">

    function urlP(path){

      document.getElementById("p").value=path;

    }

   </script>
</head>
<body>

<form action="对应的路径"  method="post">

<input type="file" name="upload" onchange="urlP(this.value)">

<input type="hidden" name="p" id="p">

</form>
</body>
</html>

SpringMVC框架中的对应的Controller方法:

@RequestMapping("/save")
public void save(MultipartFile upload,String p,HttpServletRequest request) throws IOException {
        if(upload!=null){
            String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");//先获取项目的发布到的路径,再拼接出上传的文件所存放的路径
            FileUtils.copyInputStreamToFile(upload.getInputStream(), new File(realPath, upload.getOriginalFilename()));//保存文件到指定位置
            String filePath=realPath+"\\"+p;    //只获取文件名以及扩展名
            audition.setAnnuxPath(filePath);
            auditionService.save(audition);
        }

}

posted @ 2014-01-09 16:10  将夜  阅读(309)  评论(0编辑  收藏  举报