java web开发,上传图片并读取

要求:
1.上传部分:使用javascript对文件类型进行简单判断,然后上传。上传可以先不写入数据库,可以是把图片新建到某个文件夹中。
2.上传后从文件夹读取,做成类似QQ空间相册一样,点击图片左边箭头上一张,右边箭头下一张,头尾两张不能点。并且图片内容变换时,页面地址栏地址是不变的。
= = 谁说上传的文件是统一命名了。。。

java web开发中,使用文件操作类来上传图片并读取,如下代码:

 * @desc: å›¾ç‰‡å¤„理工具
 * @author: bingye
 * @createTime: 2015-3-17 ä¸‹åˆ04:25:32
 * @version: v1.0
 */
public class ImageUtil {
 
    /**
    * å°†å›¾ç‰‡å†™åˆ°å®¢æˆ·ç«¯
    * @author: bingye
    * @createTime: 2015-3-17 ä¸‹åˆ04:36:04
    * @history:
    * @param image
    * @param response void
    */
    public static void writeImage(byte[] image,HttpServletResponse response){
        if(image==null){
            return;
        }
        byte[] buffer=new byte[1024];
        InputStream is=null;
        OutputStream os=null;
        try {
            is=new ByteArrayInputStream(image);
            os=response.getOutputStream();
            while(is.read(buffer)!=-1){
                os.write(buffer);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                if(is!=null){is.close();}
                if(os!=null){os.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
     
    /**
    * èŽ·å–指定路劲图片
    * @author: bingye
    * @createTime: 2015-3-21 ä¸Šåˆ10:50:44
    * @param filePath
    * @param response void
    */
    public static void writeImage(String filePath,HttpServletResponse response){
        File imageFile=new File(filePath); 
        if(imageFile!=null && imageFile.exists()){
            byte[] buffer=new byte[1024];
            InputStream is=null;
            OutputStream os=null;
            try {
                is=new FileInputStream(imageFile);
                os=response.getOutputStream();
                while(is.read(buffer)!=-1){
                    os.write(buffer);
                    os.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                try {
                    if(is!=null){is.close();}
                    if(os!=null){os.close();}
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
     
    /**
    * å›¾ç‰‡ä¸Šä¼ åˆ°æ–‡ä»¶å¤¹
    * @author: bingye
    * @createTime: 2015-3-20 ä¸‹åˆ08:07:25
    * @param file
    * @param savePath
    * @return boolean
    */
    public static ResultDto uploadToLocal(CommonsMultipartFile file,String savePath){
        if(file!=null && !file.isEmpty()){
            //获取文件名称
            String fileName=file.getOriginalFilename();
            //获取后缀名
            String suffixName=fileName.substring(fileName.indexOf(".")+1);
            //新名称
            String newFileName=System.currentTimeMillis()+"."+suffixName;
            //新文件路劲
            String filePath=savePath+newFileName;
            //获取存储文件路径
            File fileDir=new File(savePath);
            if(!fileDir.exists()){
                //如果文件夹没有:新建
                fileDir.mkdirs();
            }
            FileOutputStream fos=null;
            try {
                fos=new FileOutputStream(filePath);
                fos.write(file.getBytes());
                fos.flush();
                return ResultUtil.success("UPLOAD_SUCCESS", URLEncoder.encode(newFileName,"utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.fail("UPLOAD_ERROR");
            } finally{
                try {
                    if(fos!=null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultUtil.fail("UPLOAD_ERROR");
                }
            }
        }
        return ResultUtil.fail("UPLOAD_ERROR");
    }
     
     
     
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-08-31
struts2的文件上传一定容易到让你吃惊!至于第2的那种效果,用JavaScript实现本回答被提问者采纳
第2个回答  2009-08-31
上传的话简单:
用smartUpload
新建doUpload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:directive.page import="java.io.File" />
<HTML>
<BODY>
<%

//参数path:web发布根路径。
String path = request.getSession().getServletContext().getRealPath("");
//参数savePath:上传文件存放路径。
String savePath = "\\temp\\";
savePath = path + savePath;
//这里的路径可以自己设定
//如果savePath不存在,就新建一个文件夹
File tempPath = new File(savePath);
if (!tempPath.exists()) {
tempPath.mkdir();
}

// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// su.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
int count = su.save(savePath);
//out.println(count + "个文件!<br>");
// 逐一提取上传文件信息,同时可保存文件。
com.jspsmart.upload.File file = su.getFiles().getFile(0);
// 若文件不存在则继续

%>
</BODY>
</HTML>
2,读取文件就更简单了
假如你所有的文件都存到指定路径下
文件命名规则比如“imge1,imge2..........”很多
frame.jsp

<body>
<div id="frame">
<input name=lastime type=Button value="<--" onclick="lastpic()">//上一张图片
<imge id="picture" src="image1.jpg"> //默认第一张图片
<input name="index" value=1 type=hidden>
<input name=lastime type=Button value="-->"onclick="nextpic()">//下一张图片

</div>
</body>

<script type="text/javascript">
function lastpic()
{
var index = document.getElementById("index").value
if(index>1)
document.getElementById("index").src = "image"+(index-1)+".jpg";//上一张图片
}

function nextpic()
{
var index = document.getElementById("index").value
if( document.getElementById("image"+(index+1)+".jpg")
document.getElementById("index").src = "image"+(index+1)+".jpg";//下一张图片

}
</script>
第3个回答  2009-08-31
按照你的想法效率会很低。
可以考虑用数据库的BLOB类型
相似回答