sruts2上传问题。看的是浪曦的视频
刚接触这个。不怎么会玩。求各位指教下,我按照视频的步骤做下来。结果缓存没有我上传的文件
严重: 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)
错误提示太多了超一万字了。。。。
action
[code=Java]package 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>
[/code]
------解决方案--------------------
这个还没学到!帮顶……
[解决办法]
找不到文件呗. 你看看destFile 这个文件是不是有的
[解决办法]
你没那个文件当然接收文件输入流接不到就出错了
错误讯息就是文件找不到呀
还是查找下文件放在那里及位置设置
[解决办法]
一下代码是我做上传的一个例子,你可以参照一下:
jsp代码:
- HTML code
<input type="file" name="file" id="imgurl" style="border:1px solid #7F9DB9;"/>