到底如何解决多个上传框同名的问题(注意不是上传文件的同名),郁闷几个月了,继续“500分”求助!!!!!!!!!!
如何解决多个上传框同名的问题(注意不是上传文件的同名) ,类似下面的 :
<input type= "file " name = "ouhn ">
<input type= "file " name = "ouhn ">
<input type= "file " name = "ouhn ">
……………………
<input type= "file " name = "ouhncom ">
<input type= "file " name = "ouhncom ">
<input type= "file " name = "ouhncom ">
……………………
互联网都快被我翻遍了, 还是没找到理想的解决方法 !!!
难道那些写组件的都没意识到这个问题吗? 纳闷 ……
继续求助高手, 此帖为第4个100 分,共500分,一分不会少。
先谢谢各位……
[解决办法]
lz的问题是:上传框同名无法上传吗?
我试了一下没有问题,我使用的是commons-fileupload-1.0组件,同时上传6个文件。
******************* test.jsp ********************
<%@page contentType= "text/html; charset=GBK "%>
<html>
<head>
<title> jsp1 </title>
</head>
<body bgcolor= "#ffffff ">
<form action= "Servlet " method= "post " enctype= "multipart/form-data ">
<input type= "file " name= "a ">
<br>
<input type= "file " name= "a ">
<br>
<input type= "file " name= "a ">
<br>
<hr>
<input type= "file " name= "b ">
<br>
<input type= "file " name= "b ">
<br>
<input type= "file " name= "b ">
<br>
<input type= "submit " name= "Submit " value= "upload " />
</form>
</body>
</html>
******************** Servlet.java **********************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Servlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//获得服务器应用程序所在的绝对路径
String realPath = this.getServletContext().getRealPath(this.
getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf( "\\ "));
String uploadPath = realPath + "\\upload\\ "; // 用于存放上传文件的目录
String tempPath = realPath + "\\upload\\temp "; // 用于存放临时文件的目录
//创建目录
File uploadPathDir = new File(uploadPath);
if (!uploadPathDir.exists()) {
uploadPathDir.mkdirs();
}
File tempPathDir = new File(tempPath);
if (!tempPathDir.exists()) {
tempPathDir.mkdirs();
}
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理表单域:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) { //确定是否为普通表单文本域
String fieldName = fi.getFieldName(); //取得文本域name
String fieldValue = fi.getString(); //取得文本域value
out.print( "name: " + fieldName + " value: " + fieldValue);
out.print( " <br> ");
} else { //开始处理文件
// 获得文件名,这个文件名包括绝对路径:
String fileName = fi.getName();
if (fileName != null) {
//截取文件扩展名
String expandName = fileName.substring(fileName.
lastIndexOf( ". "), fileName.length());
//生成随机文件名
String sRand = " ";
//当前时间
SimpleDateFormat df = new SimpleDateFormat(
"yyyyMMddHHmmss ");
sRand = df.format(new Date());
//6位随机数
Random random = new Random();
for (int j = 0; j < 6; j++) {
String rand = Integer.toString(random.nextInt(10));
sRand += rand;
}
fileName = sRand + expandName;
fi.write(new File(uploadPath + fileName));
}
}
}
} catch (Exception e) {
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
[解决办法]
file表单域同名的Struts文件上传好象不大好实现,我现在还没试出来。要不lz就自己用commons-fileupload实现一下file域的同名文件上传?
不过Struts也是使用commons-fileupload组件实现上传的,只是经它封装以后的方法实现不同了。
下面的代码试了一下,同名的file域也只能上传一个文件。
public ActionForward execute(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
String dir = servlet.getServletContext().getRealPath( "/upload ");
Hashtable hm = arg1.getMultipartRequestHandler().getFileElements();
Set set = hm.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String key = (String) it.next();
FormFile file = (FormFile) hm.get(key);
String fname = file.getFileName();
System.out.println( "key= " + key + " filename= " + fname);
if (file == null) {
arg3.getWriter().print( "upload fail ");
return null;
}
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/ " + fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
file.destroy();
}
return null;
}
[解决办法]
这个问题用struts解决很简单,可以在网上找到代码,基本思路是:
定义一个类 myFile
包括一个属性如:pirvate FormFile file
在ActionForm里添加一个属性:
private List uploadfiles;
定义一个特别的方法为:()
public viod getUploadfiles(index){
就是这个方法,我不记得了,你得上网找,它实现的是从页面中自动的将文件上传到uploadfiles数组里,就几行代码而已
}
public void reset(){
uploadfiles = new ArrayList();
uploadfiles.add(new myFile());
uploadfiles.add(new myFile());
uploadfiles.add(new myFile());
}
在页面利用 <nested:iterate> 将List输出到页面。
以上代码中有很多错误,但思路就是这样的。很多我也记不住,反正我的项目里用过这种方法,已成功。
[解决办法]
手头只有1.1的struts,看了一下源码,你这问题好像无解
FormFile.java
=======================================
* ... ... The actual structure of the FormFile is dependant
* on the underlying impelementation of multipart request handling. The default implementation
* that struts uses is org.apache.struts.upload.CommonsMultipartRequestHandler.
CommonsMultipartRequestHandler.java
=======================================
/**
* Adds a file parameter to the set of file parameters for this request
* and also to the list of all parameters.
*
* @param item The file item for the parameter to add.
*/
protected void addFileParameter(FileItem item) {
FormFile formFile = new CommonsFormFile(item);
elementsFile.put(item.getFieldName(), formFile);
elementsAll.put(item.getFieldName(), formFile);
}
/**
* The file request parameters.
*/
private Hashtable elementsFile;
[解决办法]
你可以试试看把struts的这里改改,不能用同名的field name来做hashCode
------------------------
String hc = item.getFieldName().concat(elementsFile.size())
elementsFile.put(hc, formFile);
elementsAll.put(hc, formFile);
[解决办法]
一样的.
<input type= "file " name = "ouhn ">
<input type= "file " name = "ouhn ">
<input type= "file " name = "ouhn ">
跟
<html:file property= "ouhn " name= "ActionForm "/>
<html:file property= "ouhn " name= "ActionForm "/>
<html:file property= "ouhn " name= "ActionForm "/>
生成的html是一样的.只不过ouhn要定义成树组.经测试没问题.有问题联系.
对Js的操作没有任何影响.
[解决办法]
为什么不反过来想呢,不要让他同名应该可以做得到吧
[解决办法]
<javascript网页开发> -张孝祥
第206页 有一个类似的例子 但是不能复制 你下载下来看看
如果找不到 可以QQ我
MY QQ IS:10520907