文件上传

  相信有过Web开发经历的小朋友,对于文件上传都不陌生,但如何在自己的网站上提供文件上传呢?我想一定难到过很多同学,我就是其中的一个,近段时间因为开发的需要,总结了一下Web端文件上传的原理,在这里和大家分享一下。

jsp页面的主要代码:

<body>
    <h1>单文件上传</h1>
    <hr/>
    <form action="upload" method="post" enctype="multipart/form-data">
    文件<input type="file" name="img1"/><br/>
    <input type="submit" value="上传"/>
    </form>
    <h1>文件重命名</h1>
    <hr/>
    <form action="upload2" method="post" enctype="multipart/form-data">
    文件名<input type="text" name="imgname"/><br/>
    文件<input type="file" name="img1"/><br/>
    <input type="submit" value="上传"/>
    </form>
    <h1>自动生成文件名</h1>
    <hr/>
    <form action="upload3" method="post" enctype="multipart/form-data">
    文件<input type="file" name="img1"/><br/>
    <input type="submit" value="上传"/>
    </form>
    <h1>多文件上传</h1>
    <hr/>
    <form action="upload4" method="post" enctype="multipart/form-data">
    文件1<input type="file" name="img1"/><br/>
     文件2<input type="file" name="img1"/><br/>
      文件3<input type="file" name="img1"/><br/>
    <input type="submit" value="上传"/>
    </form>
  </body>

  下面是具体的上传方法介绍 

单文件上传:
 SmartUpload smart = new SmartUpload();
  smart.initialize(this.getServletConfig(),request,response);   try { smart.upload(); smart.save("images");     } catch (SmartUploadException e) {   // TODO Auto-generated catch block   e.printStackTrace(); }

 文件重命名:

SmartUpload smart = new SmartUpload();
		smart.initialize(this.getServletConfig(),request,response);
		try {
			smart.upload();
			String imgname = smart.getRequest().getParameter("imgname");
			
			String absPath = this.getServletContext().getRealPath("/images");//获取文件的保存路径
			System.out.println(absPath);
			String ext = smart.getFiles().getFile(0).getFileExt();//获取文件的后缀名
		
			String fileName = absPath+File.separator+imgname+"."+ext;//生成文件的保存名
			System.out.println("File name="+fileName);
			
			smart.getFiles().getFile(0).saveAs(fileName);
			
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
自动生成文件名:
SmartUpload smart = new SmartUpload();
		smart.initialize(this.getServletConfig(),request,response);
		
		String disFile = "";
		try {
			smart.upload();
			//String imgname = smart.getRequest().getParameter("imgname");
			IPTimeStamp ipts = new IPTimeStamp();
			String imgname = ipts.getIPTimestamp();
			
			String absPath = this.getServletContext().getRealPath("/images");
			System.out.println(absPath);
			String ext = smart.getFiles().getFile(0).getFileExt();
		
			String fileName = absPath+File.separator+imgname+"."+ext;
			System.out.println("File name="+fileName);
			
			smart.getFiles().getFile(0).saveAs(fileName);
			disFile = imgname+"."+ext;
			
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

生成随机文件名的代码:

package cn.edu.hpu.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class IPTimeStamp {
	
	private String ip;
	
	public IPTimeStamp()
	{
		
	}
	public IPTimeStamp(String ip)
	{
		this.ip = ip;
	}
	
	public String getIPTimestamp()
	{
		StringBuffer buffer = new StringBuffer();
		
		if(ip != null)
		{
			String [] digits = ip.split("\\.");
			for(String s : digits)
			{
				buffer.append(s);
			}
		}
		
		//时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String time = sdf.format(new Date());
		buffer.append(time);
		
		Random random = new Random();
		for(int i=0; i<4; i++)
		{
			buffer.append(random.nextInt(10));
		}
		
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		IPTimeStamp ipts = new IPTimeStamp("192.168.19.121");
		System.out.println(ipts.getIPTimestamp());
	}

}
多文件上传:
SmartUpload smart = new SmartUpload();
		smart.initialize(this.getServletConfig(),request,response);
		try {
			smart.upload();
			for(int i=0; i<smart.getFiles().getCount(); i++)
			{
				//String imgname = smart.getRequest().getParameter("imgname");
				IPTimeStamp ipts = new IPTimeStamp();
				String imgname = ipts.getIPTimestamp();
				
				String absPath = this.getServletContext().getRealPath("/images");//文件保存的路径
				String ext = smart.getFiles().getFile(i).getFileExt();//获得文件的后缀名
			
				String fileName = absPath+File.separator+imgname+"."+ext;//生成上传文件的保存时的名字
				System.out.println("File name="+fileName);
				
				smart.getFiles().getFile(i).saveAs(fileName);
			}
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

  相信看到这里,大家一定已经明白如何完成文件上传了。(如有错误还望指正。谢谢)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。