MVC 文件上传

来源:互联网 发布:新东方英语软件下载 编辑:程序博客网 时间:2024/04/29 01:15
JSP页面:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<% response.setContentType("text/html");
//   图片上传路径
   String uploadPath =request.getSession().getServletContext().getRealPath("/")+"upload/images/";
//   图片临时上传路径
   String tempPath = request.getSession().getServletContext().getRealPath("/")+"upload/images/temp/";
//   图片网络相对路径
   String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
//   文件夹不存在就自动创建:
   if(!new File(uploadPath).isDirectory())
   new File(uploadPath).mkdirs();
   if(!new File(tempPath).isDirectory())
   new File(tempPath).mkdirs();
   try {
   DiskFileUpload fu = new DiskFileUpload();
//   设置最大文件尺寸,这里是4MB
   fu.setSizeMax(4194304);
//   设置缓冲区大小,这里是4kb
   fu.setSizeThreshold(4096);
//   设置临时目录:
   fu.setRepositoryPath(tempPath);
//   得到所有的文件:
   List fileItems = fu.parseRequest(request);
   Iterator i = fileItems.iterator();
//   依次处理每一个文件:
   while(i.hasNext()) {
   FileItem file = (FileItem)i.next();
//   获得文件名,这个文件名是用户上传时用户的绝对路径:
   String sourcefileName = file.getName();
   if(sourcefileName!=null&&(sourcefileName.endsWith(".jpg")||sourcefileName.endsWith(".gif"))) {
//   在这里可以记录用户和文件信息,生成上传后的文件名
   String destinationfileName=null;
   Random rd = new Random();
   Calendar time = Calendar.getInstance();
   if(sourcefileName.endsWith(".jpg")){
   destinationfileName=String.valueOf(time.get(Calendar.YEAR))
   + String.valueOf(time.get(Calendar.MONTH))
   + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
   + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
   + String.valueOf(time.get(Calendar.MINUTE))
   + String.valueOf(time.get(Calendar.SECOND))
   + String.valueOf(rd.nextInt(100)) + ".jpg";
   }else if(sourcefileName.endsWith(".gif")){
   destinationfileName=String.valueOf(time.get(Calendar.YEAR))
   + String.valueOf(time.get(Calendar.MONTH))
   + String.valueOf(time.get(Calendar.DAY_OF_MONTH))
   + String.valueOf(time.get(Calendar.HOUR_OF_DAY))
   + String.valueOf(time.get(Calendar.MINUTE))
   + String.valueOf(time.get(Calendar.SECOND))
   + String.valueOf(rd.nextInt(100)) + ".gif";
   }
   File f1=new File(uploadPath+ destinationfileName);
   file.write(f1);
   //sourcefileName
   //out.print("成功上传!") ;
   request.getSession().getServletContext().setAttribute("mainpath","upload////images////"+destinationfileName);
   request.getSession().getServletContext().setAttribute("fillpath",  uploadPath+destinationfileName);
   System.out.println(request.getSession().getServletContext().getAttribute("mainpath"));
   out.print("<img width=100px,height=100px src="+imagePath+"upload/images/"+destinationfileName+">");
   }else{
   out.println("上传文件出错,只能上传 *.jpg , *.gif");
   }
   }
//   跳转到上传成功提示页面
   }
   catch(Exception e) {
//   可以跳转出错页面
   }
   out.flush();
   out.close();

%>

HTML测试页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<script src="<%=request.getContextPath() %>/js/jquery-1.9.1.js" type="text/javascript" ></script> 
<script src="<%=request.getContextPath() %>/js/ajaxfileupload.js" type="text/javascript" ></script> 
<body>
<!-- 上传文件程序应用示例 -->
<%--
类型enctype用multipart/form-data,这样可以把文件中的数据作为流式数据上传,不管是什么文件类型,均可上传。
--%>
<form name="form1" id="form1" target="hiddeniframe" action='upload/uploadfile.jsp' method='post' enctype='multipart/form-data'>
<label>请选择头像:</label>
<!--  当选中图片时,将触动onchange方法提交,并且将图片根据target指定iframe显示出来  --> 
<input type='file' name='upfile' size='50' onchange="fsubmit()">
<hr>
<iframe id="hiddeniframe" name="hiddeniframe" ></iframe>
</form>
<script type="text/javascript">
function fsubmit(){
  
  document.getElementById("form1").submit();
  
  }
</script>
</body>
</html> 
0 0
原创粉丝点击