文件上传 smartUpload
文件上传
martUpload
步骤如下:
1、实例化smartUplpad对象 SmartUpload smart = new SmartUpload();
2、初始化上传的操作 smart.initalize(pageContext);
3、准备上传 smart.upload();
4、保存文件 smart.save("upload");
如果要进行文件的上传,则肯定要对表单进行封装,,但是表单一旦封装后无法使用request.getParameter()接受参数;
实现文件上传的代码:
form表单代码:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="smartupload_demo02.jsp" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="uname"><br> 12 请选择要上传的文件:<input type="file" name="pic" /><br> 13 <input type="submit" value="上传"> 14 <input type="reset" value="重置"> 15 </form> 16 </body> 17 </html> 18 19
上传文件代码:
1 <%@page import="com.jspsmart.upload.SmartUpload"%> 2 3 <%@page import="javax.servlet.ServletException" %> 4 <%@page import="java.io.File" %> 5 <%@ page language="java" contentType="text/html; charset=UTF-8" 6 pageEncoding="UTF-8"%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 <% String realPath = application.getRealPath("");%> 15 <%out.print(realPath); 16 File file = new File(realPath+"/upload"); 17 file.mkdir(); 18 SmartUpload smart = new SmartUpload(); 19 smart.initialize(pageContext) ; // 初始化上传操作 20 smart.upload() ; // 上传准备 21 smart.save("upload") ; // 文件保存 */ 22 %> 23 </body> 24 </html> 25 26
以下为:图片自动命名 IP+shijian+三位随机数
usebean代码:
1 package com.yunshidai.zhm.smartupload; 2 3 import java.io.File; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Random; 7 8 public class IPTimeStamp { 9 private SimpleDateFormat sdf = null ; 10 private String ip = null; 11 public IPTimeStamp(){ 12 13 } 14 public IPTimeStamp(String ip){ 15 this.ip = ip ; 16 } 17 public String getIPTimeRand(){ 18 StringBuffer buf = new StringBuffer(); 19 if (this.ip !=null) { 20 String s[] = this.ip.split("\\."); 21 for (int i = 0; i < s.length; i++) { 22 buf.append(this.addZero(s[i], 3)); 23 } 24 } 25 buf.append(this.getTimeStamp()); 26 Random r = new Random(); 27 for (int i = 0; i < 3; i++) { 28 buf.append(r.nextInt(10)); 29 } 30 return buf.toString(); 31 } 32 public String getTimeStamp(){ 33 this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 34 return this.sdf.format(new Date()); 35 } 36 public String getDate(){ 37 this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 38 return this.sdf.format(new Date()); 39 } 40 private String addZero(String str,int len){ 41 StringBuffer s = new StringBuffer(); 42 s.append(str); 43 while(s.length()<len){ 44 s.insert(0, "0"); 45 } 46 return str.toString(); 47 } 48 public static void main(String[] args) { 49 System.out.println(new IPTimeStamp("192.168.1.1").getIPTimeRand()); 50 } 51 } 52 53
form表单代码:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="smartupload_demo03.jsp" method="post" enctype="multipart/form-data"> 11 姓名:<input type="text" name="uname"><br> 12 请选择要上传的文件:<input type="file" name="pic" /><br> 13 <input type="submit" value="上传"> 14 <input type="reset" value="重置"> 15 </form> 16 </body> 17 </html>
文件上传代码:
1 <%@page import="com.jspsmart.upload.SmartUpload"%> 2 <%@page import="javax.servlet.ServletException" %> 3 <%@page import="com.yunshidai.zhm.smartupload.*" %> 4 <%@page import="java.io.File" %> 5 <%@ page language="java" contentType="text/html; charset=UTF-8" 6 pageEncoding="UTF-8"%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 <% 15 SmartUpload smart = new SmartUpload(); 16 smart.initialize(pageContext) ; // 初始化上传操作 17 smart.upload() ; // 上传准备 18 String name= smart.getRequest().getParameter("uname"); 19 IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()); 20 String ext = smart.getFiles().getFile(0).getFileExt(); 21 String fileName = its.getIPTimeRand()+"."+ext ; 22 smart.getFiles().getFile(0).saveAs(this.getServletContext(). 23 getRealPath("")+"/upload"+java.io.File.separator+ 24 fileName); 25 %> 26 <h2>姓名:<%=name%></h2> 27 <%=fileName %> 28 <img src="<%=fileName%>"> 29 30 </body> 31 32 </html> 33 34
注意事项:之前要建立一个upload的文件夹
需要帮忙的解决:现在的照片给你传到服务器的文件夹中,但是在界面显示还存在问题,还没有解决。