码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现模拟QQ空间图片上传

时间:2015-01-27 18:36:19      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:java实现qq空间上传图片




Java实现模拟QQ空间图片上传


首先看效果:



技术分享


首先编写我们的上传jsp代码,如下:


<%@ 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>Java实现开发QQ空间上传</title>
<style type="text/css">
body{background: url("image/bg_webp.jpg");}
*{padding: 0px;margin: 0px;}
.qq{width: 520px;height: 70px;margin: 200px auto;border: 1px solid #e6e6e6;background-color: #fff}
.qq .qq_pic{float: right;width: 48px;height: 70px;border-left: 1px solid #eee8d1;background-color: #fafafa;border-right: 1px solid #eee8d1 }
.qq .qq_pic .bg_button{width: 48px;height: 70px;display: block;padding: 20px 0px 0px 10px}
.qq .qq_pic .bg_button:HOVER{background-color: #fff;height: 50px;width: 38px;}
.qq .qq_pic .bg_button i{background: url("image/bg_button.png");background-position: -490px -125px;width: 25px;height: 25px;display: block;}
.qq .qq_pic input{display: none;}
</style>
</head>
<body>
	

	<form action="<%=request.getContextPath()%>/UploadQQServlet" enctype="multipart/form-data" method="post" id="saveForm">
		<div class="qq">
				<div class="qq_pic">
					<a href="#" class="bg_button" onclick="openBrowser()">
						<i></i>
					</a>	
					<input type="file" id="fileName_upload" name="file1" onchange="saveFile()">	
				</div>
		</div>
	</form>
	<div>
	<img alt="图片预览" src="upload/${fileName}" style="height: 30px;width: 30px;">
	
	</div>

<script type="text/javascript">
 function openBrowser(){
 	var boolean=navigator.appName=="Microsoft Internet Explorer"?true:false;
 	if(boolean){
 		document.getElementById("fileName_upload").click();
 	}else{
		var a=document.createEvent("MouseEvents");
		a.initEvent("click",true,true);
		document.getElementById("fileName_upload").dispatchEvent(a);
 	}
 }
 function saveFile(){
 	document.getElementById("saveForm").submit();
 }

</script>
</body>
</html>


然后编写我们的后台代码,这里我就简单使用Servlet


package com.wonders.upload;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadQQServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//获取文件路径
		String strPath=request.getRealPath("/")+"/upload";
		File file =new File(strPath);
		if(!file.exists())file.mkdirs();
		FileItemFactory factory=new DiskFileItemFactory();
		ServletFileUpload upload=new ServletFileUpload(factory);
		//从请求对象中获取文件信息
		List items;
		try {
			items = upload.parseRequest(request);
		if(items!=null){
			for(int i=0;i<items.size();i++){
				Iterator iterator=items.iterator();
				while(iterator.hasNext()){
				FileItem item=(FileItem)iterator.next();
				if(item.isFormField()){
					continue;
				}else{
					String fileName=item.getName();
					Long fileSize=item.getSize();
					int pos=fileName.indexOf(".");
					String ext=fileName.substring(pos,fileName.length());	
				 	fileName=UUID.randomUUID().toString()+ext;
				 	request.getSession().setAttribute("fileName", fileName);
				 	File saveFile=new File(strPath,fileName);
				 	item.write(saveFile);
				 	response.sendRedirect("UploadQQ.jsp");
				 }
				}
			}
		}	
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
}

ok,此代码联手而用!

下载源码:http://download.csdn.net/detail/u010506940/8401307



Java实现模拟QQ空间图片上传


首先看效果:


Java实现模拟QQ空间图片上传

标签:java实现qq空间上传图片

原文地址:http://blog.csdn.net/u010506940/article/details/43196329

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!