刚接触这个。不怎么会玩。求各位指教下,我按照视频的步骤做下来。结果缓存没有我上传的文件严重: Servlet.service() for servlet default threw exception
java.io.FileNotFoundException: F:\soft\tomcat6\work\Catalina\localhost\struts2Dome\upload__418edaad_13311433555__8000_00000002.tmp (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at com.action.LoginAction.findUser(LoginAction.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)错误提示太多了超一万字了。
actionpackage com.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.*;import org.apache.struts2.ServletActionContext;import dao.UserDao;import vo.Users;public class LoginAction{ private Users users;
private String username;
private String password;
private File file;
private String fileFileName;
private String fileContuenType;



public String findUser() throws Exception {
UserDao userDao = new UserDao();
String judge = userDao.findUsers(users.getUsername(), users.getPwd());
if (null != judge) {

System.out.println(file);
InputStream is = new FileInputStream(file);//接收文件输入流

String root = ServletActionContext.getRequest().getRealPath("/");

File destFile = new File(root,this.getFileFileName());//保存文件存放路径

OutputStream so = new FileOutputStream(destFile);//文件输出

byte[] buffer = new byte[1024];
int lengt =0;
while((lengt=is.read(buffer))>0){
so.write(buffer);
}
is.close();
so.close();



return "success";
} else {
return "errors";
}
}



public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public Users getUsers() {
return users;
} public void setUsers(Users users) {
this.users = users;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContuenType() {
return fileContuenType;
} public void setFileContuenType(String fileContuenType) {
this.fileContuenType = fileContuenType;
}

}页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>登录页</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">
-->
  </head>
  
  <body>
  <form action="login.action" method="post" name="form1" enctype="multipart/form-data">
   useaname:<input tyep="text" name="users.username"> <br>
   password:<input type="password" name="users.pwd"> <br>
   file:<input type="file" name="file"><br>
   
   <input type="submit" value="提交">
   </form>
  </body>
</html>
 

解决方案 »

  1.   

    找不到文件呗. 你看看destFile 这个文件是不是有的
      

  2.   

    没到那。。
    InputStream   is   =   new   FileInputStream(file);//接收文件输入流就出错了
      

  3.   

    一下代码是我做上传的一个例子,你可以参照一下:jsp代码:<input type="file" name="file" id="imgurl" style="border:1px solid #7F9DB9;"/>
    action代码://首先将文件写入一个输入流里面
                InputStream is = new FileInputStream(file);
                //其次得到你要上传文件到那个目录
                String root = ServletActionContext.getRequest().getRealPath("/upload/images");
                DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                String formatDate = format.format(new Date());
                int random = new Random().nextInt(10000);
                int position = fileFileName.indexOf(".");
                String extension = fileFileName.substring(position) ;
                String newFileName = formatDate + random + extension ;
                //再次创建一个File来保存你的文件
                File destFile = new File(root,newFileName);
                //然后就是一个输出流将文件写入到File中。
                OutputStream os = new FileOutputStream(destFile);
                //以下就是写入文件的方式
                byte buffer[] = new byte[2048]; 
                int length = 0;
                while((length = is.read(buffer))>0){
                    os.write(buffer,0,length);
                }
                //最后一定要关闭流 
                is.close();
                os.close();
    经过测试可以完全通过的……不过这是一个最基本的struts上传示例……
      

  4.   

    java.io.FileNotFoundException:   F:\soft\tomcat6\work\Catalina\localhost\struts2Dome\upload__418edaad_13311433555__8000_00000002.tmp   (系统找不到指定的文件。) 
    是不这个文件路径写错了???