java文件上传(如图片)到以二进制的方式存入Oracle或mysql数据库,还可从数据库取出展示,来个demo 200
展开全部
@RequestMapping("/upload")
public String add(@RequestParam("file") CommonsMultipartFile[] files,HttpServletRequest request){
for(int i = 0;i<files.length;i++){
System.out.println("fileName---------->" + files[i].getOriginalFilename());
if(!files[i].isEmpty()){
int pre = (int) System.currentTimeMillis();
try {
//拿到输出流,同时重命名上传的文件
FileOutputStream os = new FileOutputStream("F:/demo/" + new Date().getTime() + files[i].getOriginalFilename());
//拿到上传文件的输入流
FileInputStream in = (FileInputStream)files[i].getInputStream();
//以写字节的方式写文件
int b = 0;
while((b=in.read()) != -1){
os.write(b);
}
os.flush();
os.close();
in.close();
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传出错");
}
}
}
return "/success";
}
@RequestMapping("/upload2" )
public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException {
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//记录上传过程起始时的时间,用来计算上传时间
int pre = (int) System.currentTimeMillis();
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println(myFileName);
//重命名上传后的文件名
String fileName = "demoUpload" + new Date().getTime()+file.getOriginalFilename();
//定义上传路径
String path = "F:/demo/" + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
//记录上传该文件后的时间
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
}
}
return "/success";
}
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/front/jquery-2.1.1.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
i = 1;
j = 1;
$(document).ready(function(){
$("#btn_add1").click(function(){
document.getElementById("newUpload1").innerHTML+='<div id="div_'+i+'"><input name="file" type="file" /><input type="button" value="删除" onclick="del_1('+i+')"/></div>';
i = i + 1;
});
$("#btn_add2").click(function(){
document.getElementById("newUpload2").innerHTML+='<div id="div_'+j+'"><input name="file_'+j+'" type="file" /><input type="button" value="删除" onclick="del_2('+j+')"/></div>';
j = j + 1;
});
});
function del_1(o){
document.getElementById("newUpload1").removeChild(document.getElementById("div_"+o));
}
function del_2(o){
document.getElementById("newUpload2").removeChild(document.getElementById("div_"+o));
}
</script>
</head>
<body>
<h1>springMVC字节流输入上传文件</h1>
<form name="userForm1" action="upload" enctype="multipart/form-data" method="post">
<div id="newUpload1">
<input type="file" name="file">
</div>
<input type="button" id="btn_add1" value="增加一行" >
<input type="submit" value="上传" >
</form>
<br>
<br>
<hr align="left" width="60%" color="#FF0000" size="3">
<br>
<br>
<h1>springMVC包装类上传文件</h1>
<form name="userForm2" action="upload2" enctype="multipart/form-data" method="post"">
<div id="newUpload2">
<input type="file" name="file">
</div>
<input type="button" id="btn_add2" value="增加一行" >
<input type="submit" value="上传" >
</form>
</body>
</html>
public String add(@RequestParam("file") CommonsMultipartFile[] files,HttpServletRequest request){
for(int i = 0;i<files.length;i++){
System.out.println("fileName---------->" + files[i].getOriginalFilename());
if(!files[i].isEmpty()){
int pre = (int) System.currentTimeMillis();
try {
//拿到输出流,同时重命名上传的文件
FileOutputStream os = new FileOutputStream("F:/demo/" + new Date().getTime() + files[i].getOriginalFilename());
//拿到上传文件的输入流
FileInputStream in = (FileInputStream)files[i].getInputStream();
//以写字节的方式写文件
int b = 0;
while((b=in.read()) != -1){
os.write(b);
}
os.flush();
os.close();
in.close();
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
} catch (Exception e) {
e.printStackTrace();
System.out.println("上传出错");
}
}
}
return "/success";
}
@RequestMapping("/upload2" )
public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException {
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//记录上传过程起始时的时间,用来计算上传时间
int pre = (int) System.currentTimeMillis();
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println(myFileName);
//重命名上传后的文件名
String fileName = "demoUpload" + new Date().getTime()+file.getOriginalFilename();
//定义上传路径
String path = "F:/demo/" + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
//记录上传该文件后的时间
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
}
}
return "/success";
}
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/front/jquery-2.1.1.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
i = 1;
j = 1;
$(document).ready(function(){
$("#btn_add1").click(function(){
document.getElementById("newUpload1").innerHTML+='<div id="div_'+i+'"><input name="file" type="file" /><input type="button" value="删除" onclick="del_1('+i+')"/></div>';
i = i + 1;
});
$("#btn_add2").click(function(){
document.getElementById("newUpload2").innerHTML+='<div id="div_'+j+'"><input name="file_'+j+'" type="file" /><input type="button" value="删除" onclick="del_2('+j+')"/></div>';
j = j + 1;
});
});
function del_1(o){
document.getElementById("newUpload1").removeChild(document.getElementById("div_"+o));
}
function del_2(o){
document.getElementById("newUpload2").removeChild(document.getElementById("div_"+o));
}
</script>
</head>
<body>
<h1>springMVC字节流输入上传文件</h1>
<form name="userForm1" action="upload" enctype="multipart/form-data" method="post">
<div id="newUpload1">
<input type="file" name="file">
</div>
<input type="button" id="btn_add1" value="增加一行" >
<input type="submit" value="上传" >
</form>
<br>
<br>
<hr align="left" width="60%" color="#FF0000" size="3">
<br>
<br>
<h1>springMVC包装类上传文件</h1>
<form name="userForm2" action="upload2" enctype="multipart/form-data" method="post"">
<div id="newUpload2">
<input type="file" name="file">
</div>
<input type="button" id="btn_add2" value="增加一行" >
<input type="submit" value="上传" >
</form>
</body>
</html>
追问
这个只是个文件上传,并没有存入数据库
追答
这里是上传到服务骑,你只要把图片路径存入数据库就行了,看照片查询路径写上去就行了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
目前存2进制的很少了 如果 图片 浏览频繁 建议 整个图片服务器
追问
那存入数据库的是URL吗
追答
对是的 或是key
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询