读书人

天杀的404 登陆后下传出现404 不登陆下

发布时间: 2012-10-13 11:38:17 作者: rapoo

天杀的404 登陆后上传出现404 不登陆上传成功 不知道为神马求指教
login.jsp页面

<body>
<form action="login" method="post">
username:<input type="text" name="username"><br/>
password:<input type="password" name="password"><br/>
<input type="submit" value="submit">
</body>


struts.xml页面

<package name="zhou" extends="struts-default">
<action name="login" class="ac.LoginAction">
<result name="success">/upload.jsp</result>
<result name="input">/index.jsp</result>





</action>
<action name="upload" class="ac.FileUploadAction">
<param name="savePath">/upload</param>
<result name="success">/show.jsp</result>
<result name="input">/upload.jsp</result>

</action>



</package>



login.action页面



package ac;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username;
private String password;
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 String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
Map session = actionContext.getSession();

String url = "jdbc:mysql://localhost:3306/";
String dbName = "zt";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "123";

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url + dbName, userName, passWord);
stmt = con.createStatement();

} catch (Exception e) {
System.out.println(e.getMessage());
}
stmt = con.createStatement();
String sql = "select * from user where username='" + this.username
+ "'and password='" + this.password + "'";
rs = stmt.executeQuery(sql);

if (!rs.next()) {

return INPUT;
} else {
session.put("USER", this.username);
return SUCCESS;

}








}

}




upload.jsp




<body>
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="imgfile" label="选择图片"></s:file>
<s:text name="title"></s:text>
<s:submit value="submit"/>



</s:form>
</body>



FileUploadAction页面



package ac;

import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.*;

import org.apache.struts2.ServletActionContext;

public class FileUploadAction extends ActionSupport {
private String title;
private File imgfile;
private static final int BUFFER_SIZE=16*1024;
private String contentType;
private String fileName;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getImgfile() {
return imgfile;
}
public void setImgfile(File imgfile) {
this.imgfile = imgfile;
}
public String getContentType() {
return contentType;
}
public void setImgfileContentType(String contentType) {
this.contentType = contentType;
}
public String getFileName() {
return fileName;
}
public void setImgfileFileName(String fileName) {
this.fileName = fileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath("/")+savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception{
FileInputStream fin=new FileInputStream(getImgfile());

FileOutputStream fout=new FileOutputStream(getSavePath()+"//"+getFileName());
byte[] buffer=new byte[BUFFER_SIZE];
int len=0;
while((len=fin.read(buffer))>0){
fout.write(buffer,0,len);
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String time = formatter.format(curDate);
System.out.println(time);

//插入数据库
String url = "jdbc:mysql://localhost:3306/";
String dbName = "zt";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "123";

Connection con = null;
Statement stmt = null;
try {
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url + dbName, userName, passWord);
stmt = con.createStatement();

} catch (Exception e) {
System.out.println(e.getMessage());
}
stmt = con.createStatement();
int val=stmt.executeUpdate("insert sinfor(fileName,time) values('"+fileName+"','"+time+"')");
if (val == 0) {
return INPUT;
} else {

return SUCCESS;
}










//插入数据库








}

return INPUT;





}





}




错误描述Could not find action or result
There is no Action mapped for namespace /upload and action name . - [unknown location]








[解决办法]
<s:form action="upload.action" method="post" enctype="multipart/form-data">
<s:file name="imgfile" label="选择图片"></s:file>
<s:text name="title"></s:text>
<s:submit value="submit"/>

</s:form>

读书人网 >J2EE开发

热点推荐