您的位置:首页 > 编程语言 > Java开发

Java源码文件上传

2014-01-09 09:47 393 查看
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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: