刚从ruby转做java,分个活做照片上传,开始感觉很崩溃,以为本人菜鸟一个,一见到流什么的就感觉很牛逼的东西我怎么会啊,自学加百度,做出来了,两种方法完成,关于js预览就不上传了,留作以后备用,也能帮助下和我一样的菜鸟
jsp页面
<form action="uploadPhoto.do" method="post" enctype="multipart/form-data"> 上传照片:<input type=file name="file" id="file" onchange="javascript:upload();"> <p><div id="localImag"><img id="preview" width=-1 height=-1 style="diplay:none" /></div></p> <img id="imagep" style="display:none" src=\‘#\‘" /<%=realPath%>" width="300px;" height="350px;"/><br/> <input type="image" src="./content/template/images/btn_upto2.jpg"> //上传按钮 </form>
/**
* 照片上传
* @author sixd
* @throws IOException
* @throws ServletException
*
*/
@RequestMapping(value = "/uploadPhoto.do")
public void uploadPhoto(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception {
InputStream is = null;
OutputStream os = null;
List<Map<String,Object>> zplist = null;
String responseText = "";
String changName = "";
String realPath = "";
//String uuid = (String) request.getSession().getAttribute("uuid");
//String xhgh = (String) request.getSession().getAttribute("xhgh");
//String uuid = "10020";
String uuid = "10017";
String xhgh = "10017";
String path = request.getSession().getServletContext().getRealPath("drzp");
String fileName = file.getOriginalFilename();
try {
zplist = zpscDao.findZpInfo(uuid);
int index = fileName.lastIndexOf(".");
String endFlag = fileName.substring(index);
changName = xhgh.concat(endFlag);
System.out.println(path);
is = file.getInputStream();
os = new FileOutputStream(path+"\\"+changName);
int len = 0;
byte[] bytes = new byte[1024];
while((len = is.read(bytes))>0){
os.write(bytes, 0, len);
}
realPath = "drzp/"+changName+"";
if(zplist != null && !zplist.isEmpty()){
zpscDao.updatePhotoPath(realPath,uuid);
}else{
zpscDao.savePhotoPath(uuid,xhgh,realPath);
}
responseText="success";
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
responseText="fail";
}finally{
is.close();
os.close();
}
response.sendRedirect("content/template/zpsc/zpsc.jsp?path="+realPath+"&responseText="+responseText+"");
}方法二、
/**
* 上传照片
* @author sixd
* @throws IOException
* @throws ServletException
*
*/
@RequestMapping(value = "/uploadPhoto.do")
public void uploadPhoto(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception {
Map<String, Object> map = null;
List<Map<String,Object>> zplist = null;
String responseText = "";
//String uuid = (String) request.getSession().getAttribute("uuid");
//String xhgh = (String) request.getSession().getAttribute("xhgh");
// String uuid = "10020";
String uuid = "10017";
String xhgh = "10017";
try {
map = zpscDao.findStudentInfo(uuid);
zplist = zpscDao.findZpInfo(uuid);
if(map != null && map.size()>0){
String fileName = file.getOriginalFilename();
int index = fileName.lastIndexOf(".");
String endFlag = fileName.substring(index);
String changName = xhgh.concat(endFlag);
System.out.println(path);
File targetFile = new File(path, changName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
file.transferTo(targetFile);
String realPath = "瀵煎叆鐓х墖/"+lx+"/"+yx+"/"+zy+"/"+bj+"/"+changName+"";
if(zplist != null && !zplist.isEmpty()){
zpscDao.updatePhotoPath(realPath,uuid);
}else{
zpscDao.savePhotoPath(uuid,xhgh,realPath);
}
}
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
}
response.sendRedirect("content/template/zpsc/zpsc.jsp");
}本文出自 “骑猴上树” 博客,请务必保留此出处http://qihoushangshu.blog.51cto.com/7872138/1431938
springmvc照片上传功能,布布扣,bubuko.com
原文地址:http://qihoushangshu.blog.51cto.com/7872138/1431938