调用的地方
<a onClick="javascript:window.open('jsp/uploadphoto.jsp','myfacewindow', 'height=100,width=440, top=350, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');" style="cursor:hand;"><span class="STYLE2">上传</span></a> 推荐最佳比例为:(50px*50px)
下面是上传的界面 uploadphoto.jsp
<%@page contentType="text/html; charset=GBK" import="java.util.HashMap"%>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {
font-size: 12px;
color: #000000;
}
.STYLE2 {
color: #006699;
font-weight: bold;
}
-->
</style>
<html>
<title>上传图片</title>
<body> <br/>
<span class="STYLE2">商务客图片上传</span><br/>
<span class="STYLE1">请您选择您要上传的图片,图片推荐:50像素*50像素!使用.jpg .gif .bmp格式</span>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form id="uploadphoto_f" name="uploadphoto_f" enctype="multipart/form-data" method="post" action="../uploadphoto.do">
<tr>
<td height="41">
<input name="flag" type="hidden" value="myface" />
<input name="loadpathto" type="hidden" value="/uploadcard/myface/" />
<input name="uploadphoto" type="file" id="uploadphoto" size="40" />
<input name="upload_b" type="submit" id="upload_b" value=" 上传 " /></td>
</tr></form>
</table>
</body>
</html>
<%String msg=request.getAttribute("msg")==null?"":request.getAttribute("msg")+"";
if(!msg.equals("")){
%>
<script>
opener.document.location.href="Userinfo.do";
window.close();
</script>
<%}%>
接着是FORM 类 uploadphotoForm
package com.onlysoft.txl.user;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: fishsoft</p>
*
* @author Danny
* @version 1.0
*/
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
public class uploadphotoForm extends ActionForm {
protected FormFile uploadphoto;
public FormFile getUploadphoto() {
return uploadphoto;
}
public void setUploadphoto(FormFile theFile) {
this.uploadphoto = theFile;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded =
(Boolean) request.getAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ( (maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
errors = new ActionErrors();
errors.add(
ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("maxLengthExceeded"));
}
return errors;
}
}
这个是action类 uploadphotoAction
2
3 /** */ /**
4 * <p>Title: </p>
5 *
6 * <p>Description: </p>
7 *
8 * <p>Copyright: Copyright (c) 2006</p>
9 *
10 * <p>Company: fishsoft</p>
11 *
12 * @author Danny
13 * @version 1.0
14 */
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import org.apache.struts.action.ActionForm;
18 import org.apache.struts.action.ActionMapping;
19 import org.apache.struts.action.ActionForward;
20
21 import com.onlysoft.txl. * ;
22 import java.sql.ResultSet;
23 import java.util.Map;
24 import java.util.HashMap;
25 import java.util.ArrayList;
26 import java.util.List;
27 import com.onlysoft.txl.mail.client.SenderMail;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31 import org.apache.struts.upload.FormFile;
32 import java.io.InputStream;
33 import java.io.ByteArrayOutputStream;
34 import javax.servlet.http.HttpSession;
35 import java.io.FileOutputStream;
36 import java.io.OutputStream;
37
38 public class uploadphotoAction extends BaseAction {
39 public ActionForward execute(
40 ActionMapping mapping,
41 ActionForm form,
42 HttpServletRequest request,
43 HttpServletResponse response) throws Exception {
44 init(request);
45 if (!isLogin) {
46 return mapping.findForward("relogin");
47 }
48 String flag = request.getParameter("flag");
49 String loadpathto = request.getParameter("loadpathto");
50 String filePath = request.getRealPath("/"); //取当前系统路径
51 String pathphoto = loadpathto;
52
53
54 if (form instanceof uploadphotoForm) {//如果form是uploadsForm
55 String encoding = request.getCharacterEncoding();
56 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
57 {
58 response.setContentType("text/html; charset=GBK");//如果没有指定编码,编码格式为GBk
59 }
60 uploadphotoForm theForm = (uploadphotoForm ) form;
61 FormFile file = theForm.getUploadphoto();//取得上传的文件
62 try {
63 InputStream stream = file.getInputStream(); //把文件读入
64
65 ByteArrayOutputStream baos = new ByteArrayOutputStream();
66
67 if (file.getFileSize() > (200 * 1024)) {
68 request.setAttribute("message", "文件请控制在200K以下!");
69 return mapping.findForward("ErrorInfo");
70 }
71 String contentType = file.getContentType();
72 System.out.println("文件类别:=" + contentType);
73 String filebackstr = "";
74 if (contentType.equals("image/pjpeg")) {
75 filebackstr = ".jpg";
76 } else if (contentType.equals("image/gif")) {
77 filebackstr = ".gif";
78 } else if (contentType.equals("image/bmp")) {
79 filebackstr = ".bmp";
80 } else {
81 request.setAttribute("message", "文件格式不正确!");
82 return mapping.findForward("ErrorInfo");
83 }
84 OutputStream bos = new FileOutputStream(filePath + pathphoto +
85 userid + file.getFileName()); //建立一个上传文件的输出流
86 int bytesRead = 0;
87 byte[] buffer = new byte[8192];
88 while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
89 bos.write(buffer, 0, bytesRead); //将文件写入服务器
90 }
91 bos.close();
92 stream.close();
93 }
94 catch(Exception e){
95 System.err.print(e);
96 }
97 String sql="update sync4j_user set MYFACEPATH='" +pathphoto.replaceFirst("/","") +userid+file.getFileName()+"' where userid="+userid;
98 db.doSql(sql, db.DELETE_MODE, conn);
99
100 }else{
101 request.setAttribute("message", "上传有误");
102 return mapping.findForward("ErrorInfo");
103 }
104
105
106
107 request.setAttribute("msg", "上传成功");
108 return mapping.findForward("loadok");
109
110 }
111}
112