关于java 使用file标签上传的问题!!求高手!
应公司需求,需要做一个上传游戏icon、apk、以及一些游戏中的截图,由于截图的数量是不固定的,所以我得通过js来添加标签,我遇到的问题就是---我在我的机器上测试,上传是什么样,就是什么样,而且通过网站看也是对的,apk包能下载,图片和icon也对!!但是!!把工程发布到服务器上边之后,icon和apk包是对的,但是游戏截图就全部变成一样的了,都是最后选定的哪一张图片,而且名字也是一样的!!!下边是我添加时的jsp页面以及后台处理的servlet!!就这么点分儿了....没办法啊!!
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addGame.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function AddMore(){
var more = document.getElementById("file");
var br = document.createElement("br");
var input = document.createElement("input");
var button = document.createElement("input");
input.type = "file";
input.name = "file";
button.type = "button";
button.value = "删除";
more.appendChild(br);
more.appendChild(input);
more.appendChild(button);
button.onclick = function(){
more.removeChild(br);
more.removeChild(input);
more.removeChild(button);
};
}
function check(){
var t = document.getElementsByTagName("input");
if(document.getElementById("gamename").value == ""){
alert("请添加您的游戏名字");
return false;
}
if(document.getElementById("gameversion").value == ""){
alert("请添加您的游戏版本");
return false;
}
if(document.getElementById("gameitr").value == ""){
alert("请添加您的游戏介绍");
return false;
}
if(document.getElementById("downqx").value == ""){
alert("请添加您的下载权限");
return false;
}
for(var i=0;i <t.length;i++){
if(t[i].type=="file"&&t[i].value==""){
if(i == 1 && t[i].value=="" ){
alert("请添加您的游戏cion");
return false;
}
if(i == 2 && t[i].value=="" ){
alert("请添加您的apk");
return false;
}
alert("请添加您的游戏图片");
return false;
}
}
}
</script>
<style type="text/css">
<!--
.STYLE2 {font-size: 18px}
-->
</style>
</head>
<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String name = (String)session.getAttribute("username");
if(name == null){%>
<script type="text/javascript">
window.location.href="login.jsp";
</script>
<%}
%>
<body>
<a href="GMListServlet">返回上页</a>
<form action="AddGameServlet" method="post" enctype="multipart/form-data" onsubmit="return check()">
<table width="620" border="1">
<tr>
<td colspan="2"><span class="STYLE2">添加游戏</span></td>
</tr>
<tr>
<td width="147"><div align="right">游戏名称</div></td>
<td width="457"><input type="text" name="gamename" id="gamename"/></td>
</tr>
<tr>
<td><div align="right">游戏icon</div></td>
<td><input type="file" name="icon" id="icon"></td>
</tr>
<tr>
<td><div align="right">APK包</div></td>
<td><input type="file" name="href" id="href"></td>
</tr>
<tr>
<td><div align="right">游戏图片</div></td>
<td id="file"><input type="file" name="file"><input type="button" value="增加图片" onclick="AddMore()"></td>
</tr>
<tr>
<td><div align="right">游戏版本</div></td>
<td><input type="text" name="gameversion" id="gameversion"/></td>
</tr>
<tr>
<td><div align="right">更新日期</div></td>
<td><input type="text" name="updatedate" value="<%=sdf.format(new Date())%>" /></td>
</tr>
<tr>
<td><div align="right">游戏介绍</div></td>
<td><textarea name="gameitr" id="gameitr" rows="10" cols="50"></textarea></td>
</tr>
<tr>
<tr>
<td><div align="right">下载权限</div></td>
<td><textarea name="downqx" id="downqx" rows="5" cols="50"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="提 交"/></td>
</tr>
</table>
</form>
<br>
</body>
</html>
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
SmartUpload mySmartUpload =new SmartUpload();
LymobileDaoImpl lbd = new LymobileDaoImpl();
//没用
Lymobile lb = new Lymobile();
double file_size=0;
String fileName2="",ext="",testvar="";
String filename = "";
String gamename = "";
String gamebos = "";
String gameversion = "";
String updatedate = "";
// String href = "";
String downqx = "";
String gameitr = "";
StringBuffer sb = new StringBuffer();
String url="images/"; //应保证在根目录中有此目录的存在
//初始化
mySmartUpload.initialize(this.getServletConfig(),request,response);
//只允许上载此类文件
try {
mySmartUpload.setAllowedFilesList("icon,ico,jpg,gif,png,apk");
//上载文件
//获取表单中其他内容
mySmartUpload.upload();
Request req = mySmartUpload.getRequest();
gamename = req.getParameter("gamename");
gamebos = req.getParameter("gamebos");
gameversion = req.getParameter("gameversion");
updatedate = req.getParameter("updatedate");
//href = req.getParameter("href");
downqx = req.getParameter("downqx");
gameitr = req.getParameter("gameitr");
//mySmartUpload.save("/upfileforhotel/images/");
} catch (Exception e){
out.print("<script>");
out.println("alert('icon,ico,jpg,gif,png,apk!');");
out.print("</script>");
}
try{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
int count=0;
if (myFile.isMissing()){
out.print("<script>");
out.println("alert('请至少选择一个要上传的文件!');");
out.print("</script>");
}
else{
//String myFileName=myFile.getFileName(); //取得上载的文件的文件名
for(int i=0;i<mySmartUpload.getFiles().getCount();i++){
myFile=mySmartUpload.getFiles().getFile(i);
if(myFile.isMissing()) continue;
ext= myFile.getFileExt(); //取得后缀名
file_size=mySmartUpload.getFiles().getFile(1).getSize(); //取得文件的大小
String saveurl="";
//更改文件名,取得当前上传时间的毫秒数值
Calendar calendar = Calendar.getInstance();
//文件名
filename = String.valueOf(calendar.getTimeInMillis());
sb.append(filename).append(".").append(ext).append(";");
saveurl=request.getRealPath("/")+url;
saveurl+=filename+"."+ext; //保存路径
//保存
myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
}
String splited = sb.toString();
String[] strarray=splited.split(";");
String icon = "";
String pics = "";
List imgList = new ArrayList();
//把除了icon之外的图片存到list里边,详细页面的横向滚动要用
for (int i = 0; i < strarray.length; i++){
if(i==0){
icon = "images/"+(String)strarray[i];
} else{
pics = "images/"+strarray[i];
imgList.add(pics);
}
}
//把从strarray里边取出来的除ICON之外的图片拼成字符串,用来存到实体中
String[] strarray2=splited.split(";");
String pojopic = "";
for (int i = 2; i < strarray2.length; i++) {
pojopic += "images/"+strarray2[i]+";";
}
Integer frist = sb.indexOf(";");
Integer second = sb.indexOf(";",frist+1);
String href ="images/"+ sb.substring(frist+1, second);
String sub = String.valueOf(file_size);
double b = 1024;
DecimalFormat df=new DecimalFormat(".##");
gamebos =df.format(file_size/b/b);
lb.setDownqx(downqx);
lb.setGamebos(gamebos+"MB");
lb.setGameicon(icon);
lb.setGameitr(gameitr);
lb.setGamename(gamename);
lb.setGamepic(pojopic);
lb.setGameversion(gameversion);
lb.setHref(href);
lb.setUpdatedate(updatedate);
lb.setOrderdoa(lbd.getAllcount()+1);
int i = lbd.addGame(lb);
if (i!=0) {
response.sendRedirect("GMListServlet");
}else{
}
}
}catch (Exception e){
e.toString();
}
}