日期:2014-05-20  浏览次数:20795 次

jsp+servlet 上传图片 源码,要有注解,不然我怎么看?
要求,
1,上传到网点的目录,、/images/年/月/日/小时/
2,获得图片名称,,改变 名称 为 ‘’随机数字‘’
3,判断是否为图片格式,,大小不能超过1024*1024
4,在另一个页面。显示图片,

------解决方案--------------------
花了半个小时,写出一个,你的需求只有实现图片尺寸没实现,自己实现下(网上搜下:js获得图片尺寸),也算是拓展自己的能力。我这个程序,需要自己先在网上找个jsmartcom_zh_CN的jar包。
贴代码,jsp的
HTML code

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图片上传</title>
</head>
<body>
    <center>
    <form action="${pageContext.request.contextPath }/servlet/GetImageServlet" enctype="multipart/form-data" method="post">
        选择图片:<input type="file" name="image" /><br /><br />
        <input type="submit" value=" 提 交 " />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value=" 重 置 "/>
    </form>
    </center>
</body>
</html>

------解决方案--------------------
Servlet类的
Java code

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class GetImageServlet extends HttpServlet{
    
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletConfig config = this.getServletConfig();
        
        SmartUpload mySmartUpload = new SmartUpload();//上传图片的工具类
        mySmartUpload.initialize(config, request, response);// 初始化
        try {
            mySmartUpload.upload();// 上传
            com.jspsmart.upload.File f1 = mySmartUpload.getFiles().getFile(0);//因为只一次只上传一个图片,所以就getFile(0),如果多次还要迭代遍历
            String imageName = f1.getFileName();//得到图片的名字
            int idx = imageName.lastIndexOf(".");
            String imageType = imageName.substring(idx, imageName.length());// 得到图片的类型,比如是 .jpg
            Calendar cal=Calendar.getInstance(); //处理时间的一个类
            int year = cal.get(Calendar.YEAR);// 得到当前年
            int month = cal.get(Calendar.MONTH )+1;//月
            int day = cal.get(Calendar.DAY_OF_MONTH);//日
            int hour = cal.get(Calendar.HOUR_OF_DAY);//小时
            
            String newImageName = String.valueOf(System.currentTimeMillis());//图片的新名字,最好不要用随机数,因为随机数也可能有一样的
            String path = "E:"+File.separator+"image"+File.separator+year+File.separator+month+File.separator+day+File.separator+hour;
            File file = new File(path);
            if(!file.exists()){//如果不存在这个路径
                file.mkdirs();//就创建
            }
            String imagePath = path+File.separator+newImageName+imageType;//已经保存的图片的绝对路径,下面要对图片重新命名
            //生成保存图片的路径,File.separator是个跨平台的分隔符
            f1.saveAs(imagePath);// 保存图片到这个目录下
            
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }
}