上传文件同名应该如何处理 数组能解决这个问题的吧.在action中用这个同名的数组接受. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 晕,我的代码还是在网上找的呢,外泄什么啊,呵呵下面是Action层代码,没有logic层,注释也不是我写的这样能实现文件上传,但如果上传同名的文件,就会在服务器端把原来的文件覆盖掉,不知道这个问题如何解决,是改文件名吗?还是用文件夹分类?package actions;import java.io.*;import javax.servlet.http.*;import org.apache.struts.action.*;import org.apache.struts.upload.FormFile; import forms.UpLoadForm;/*** <p>Title:UpLoadAction</p>* <p>Description: QRRSMMS </p>* <p>Copyright: Copyright (c) 2004 jiahansoft</p>* <p>Company: jiahansoft</p>* @author wanghw* @version 1.0*/public class UpLoadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (form instanceof UpLoadForm) {//如果form是uploadsForm String encoding = request.getCharacterEncoding(); if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312 } UpLoadForm theForm = (UpLoadForm ) form; FormFile file = theForm.getTheFile();//取得上传的文件 try { InputStream stream = file.getInputStream();//把文件读入 String filePath = request.getRealPath("/");//取当前系统路径 System.out.println(filePath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream bos = new FileOutputStream(filePath + "/" + file.getFileName());//建立一个上传文件的输出流 //System.out.println(filePath+"/"+file.getFileName()); int bytesRead = 0; byte[] buffer = new byte[8192]; while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead);//将文件写入服务器 } bos.close(); stream.close(); }catch(Exception e){ System.err.print(e); } //request.setAttribute("dat",file.getFileName()); return mapping.findForward("display"); } return null; }} 我是这么做的, 一般图片的话,file另存为 img + 相关的数据库table 的主键.这样的话,每个图片就是唯一的了。如果是电影的画就用文件夹分类应该好一些。 你的文件名有没有意义的,如果没有就用时间来作为图片的名称就OK了,如是有意义,可以参考fckeditor,他的文件上传后如果出现同名会这样改的如:fangbiao.txt ,就会改为fangbiao[1].txt,以此类推 下载某个防采集网站的几百万静态页面的办法 struts2整合spring的问题~大哥们,有空请回答~谢谢 弱弱的问一下,servlet中怎么获得form的action值 神奇的tomcat异常信息 早上刚面试的题目 有一道不知道怎么下手 求解 望weblogic8.1部署了第一个web project就出现问题了,大哥大姐们帮帮忙,在线等,,解决了就给分!!!!!! struts 中 使用下拉列表框中的问题 急,我是新手 JAVA程序的服务端怎么打包呀?救急 CMP与BMP的问题~~ 关于resultset嵌套循环的问题 寻找有JAVA EE 架构师
不知道这个问题如何解决,是改文件名吗?还是用文件夹分类?package actions;import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile; import forms.UpLoadForm;/**
* <p>Title:UpLoadAction</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (form instanceof UpLoadForm) {//如果form是uploadsForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
UpLoadForm theForm = (UpLoadForm ) form;
FormFile file = theForm.getTheFile();//取得上传的文件
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = request.getRealPath("/");//取当前系统路径
System.out.println(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/" +
file.getFileName());//建立一个上传文件的输出流
//System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
//request.setAttribute("dat",file.getFileName());
return mapping.findForward("display");
}
return null;
}
}
这样的话,每个图片就是唯一的了。
如果是电影的画就用文件夹分类应该好一些。
如:fangbiao.txt ,就会改为fangbiao[1].txt,以此类推