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空间图片上传


首先看效果:


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