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);
}
}
<%@ 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);
}
}
相关文章推荐
- JAVA-线程安全性
- java读取某个目录下所有的文件
- java线程安全总结
- 不使用myeclipse注册机得到myeclipse注册码的方法(myeclipse序列号)
- Struts 2框架有两个核心配置文件:struts.xml和struts.properties
- Ubuntu 11.04 下安装配置 JDK 7
- java泛型详解
- 如何把一个java工程打成一个jar包
- Spring Security 3.0 多页面登录配置
- myeclipse 8.5最新注册码
- java线程dump分析
- java反射详解
- Java垃圾回收精粹——权衡点、对象生命周期以及全局暂停事件
- java自带的控制面板
- Java - 如何删除文件夹
- 更新整理本人所有博文中提供的代码与工具(Java,2014.01)
- Java串口通信——Notepad与Eclipse之迷
- 计数排序
- MyEclipse连接MySQL数据库报错解决办法
- Eclipse插件安装方式及使用说明