我写的程序,系统Start后,执行上传没有任何错误,如果是同局域网的其他机器来访问的话,那么就会报错:“java.io.FileNotFoundException: d:\我的文档\桌面\123.txt (系统找不到指定的文件。)”  这样的类似的错误,我想我写的代码是不管怎么样都会读取我本机的路径下文件,而不会去读取访问我系统人的机器上文件代码如下:import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;import com.sjedu.service.test.UpLoad;public class UploadFileAction { // log4j日志
private static final Logger logger = Logger.getLogger(UploadFileAction.class);
private String jRandomid;// 页面随机数,防止代理等应用缓存页面信息
    private static final int BUFFER_SIZE = 16 * 1024 ;

public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();

HttpServletResponse response = ServletActionContext.getResponse();
String filename=request.getParameter("filename");
String ofile=request.getParameter("filename");

int t1 = filename.lastIndexOf("\\");
int t2 = filename.lastIndexOf(".");
   if(t1 >= 0 && t1 < t2 && t1 < filename.length()){
   filename= filename.substring(t1 + 1, filename.length());
   }


int t3 = ofile.lastIndexOf(".");
String fileExt=ofile.substring(t3 + 1, ofile.length());
 System.out.println("fileExt---"+fileExt);  
System.out.println(filename);
//输入流   
        InputStream in =new FileInputStream(ofile);   
        int len=in.available();
//获取file路径;


    //获取新文件名;
       
    String newDateStr=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).toString();//获取前17位
        String newRandom="";//获取后三位随机数
    int newInt=(int)(Math.random()*1000);
        if(newInt<100){
         newRandom="0"+String.valueOf(newInt);
        }else
         newRandom=String.valueOf(newInt);
        
    String newFileName=newDateStr+newRandom+"."+fileExt;
    
        File imageFile = new File(request.getSession().getServletContext().getRealPath("/")+newFileName);
      
        //上传文件       
        copy(new File(ofile), imageFile);
       
        PrintWriter pw = response.getWriter();
//返回数据(原文件名|新文件名|文件大小|后缀名|)
pw.write(filename+"|"+newFileName+"|"+len+"|"+fileExt);
        
        
      
        return null;

}

  private static void copy(File src, File dst)  {
         try  {
            InputStream in = null ;
            OutputStream out = null ;
             try  {                
                in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                 byte [] buffer = new byte [BUFFER_SIZE];
                 while (in.read(buffer) > 0 )  {
                    out.write(buffer);
                } 
             } finally  {
                 if ( null != in)  {
                    in.close();
                } 
                  if ( null != out)  {
                    out.close();
                } 
            } 
         } catch (Exception e)  {
            e.printStackTrace();
        } 
    } 


public String getJRandomid() {
return jRandomid;
}
public void setJRandomid(String randomid) {
jRandomid = randomid;
}
}

解决方案 »

  1.   

    你写错了。刚学吧..上传文件不是这么处理的!你的ofile是个啥?
     String fileExt=ofile.substring(t3 + 1, ofile.length());
                 System.out.println("fileExt---"+fileExt);  
                System.out.println(filename);
                //输入流   
                InputStream in =new FileInputStream(ofile);   
                int len=in.available();
    要是这么简单,就不需要apache 的commonfileupload.jar了
      

  2.   

    既然你用struts2,给你贴一段
    import java.io.File;
    import java.io.IOException;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class test extends ActionSupport{

        private File file;//添加一个file属性    public File getFile() {
    return file;
    } public void setFile(File file) {
    this.file = file;
    } public String execute() throws Exception {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpSession session = request.getSession();
            HttpServletResponse response = ServletActionContext.getResponse();
            String destDir = request.getSession().getServletContext().getRealPath("/")+ System.currentTimeMillis();
            try {
    FileUtils.copyFile(file, new File(destDir));
    } catch (IOException e) {
    e.printStackTrace();
    }
            return SUCCESS;
        
    }}
      

  3.   

    忘了说一句,客户端提交的<input name="file" type="file" ....>
      

  4.   

    http://www.jobedu.com.cn/archives/10005.htm
    这个例子就是 你说的意思吧,我看了 在局域网的机器上访问我所做系统,执行,还是没写入 而且我点了上传按钮后 要求在本页面显示出刚上传的文件名
      

  5.   

    找不到文件这个好处理,DEBUG取到的文件路径直接在地址栏执行一下看看就知道了fileupload包能实现吧,
      

  6.   

    回答 InviteSun 你贴的代码是可以实现,上传的,谢谢我的这个页面是通过别的页面button进来,是一个弹出对话框,然后执行上传的,现在要求如何在不动这个页面的情况下,局部更新已上传的名字到这个页面, 用iframe?????
      

  7.   


    子窗口给父窗口返回值。
    但是子窗口上传文件结束又如何给父窗口返回值呢?我想应该是子窗口里在加个iframe,把上传的target设置在这个iframe上,这个iframe得到结果再去更新父窗口。不知道是不是这个意思。