一、<s:a> 怎么设置弹出新对话框啊?也就是 _blank属性
二、拿来孙鑫的 Struts2 深入详解,抄了代码 运行时没报错,可是查看上传文件的文件夹中没有发现对应上传的文件,奇怪了! 我用的是MyEclipse7.0 。好奇怪啊。返回的页面上 fileFileName dir都有,不过fileContextType 显示不出来.请高手帮忙啊!!!!具体代码:package tea;import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
public class LoadPicAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContextType;
private String uploadDir;
public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContextType() {
return fileContextType;
} public void setFileContextType(String fileContextType) {
this.fileContextType = fileContextType;
} public String getUploadDir() {
return uploadDir;
} public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
} public void validate() {
if(fileFileName==null || fileFileName.equals("")){
this.addFieldError("form1", "请选择上传照片!");
}
}
public String execute() throws Exception {
this.setUploadDir("/tphoto");
String path=ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir=new File(path);
if(!dir.exists())dir.mkdir();
BufferedOutputStream bos=null;
BufferedInputStream bis=null;
try{
FileInputStream fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(new File(dir,this.getFileFileName()));
bos=new BufferedOutputStream(fos);
byte[] buf=new byte[4096];
int len=-1;
while((len=bis.read(buf))!=-1){
bos.write(buf, 0, len);
}
}
catch (IOException e) {
this.addFieldError("file", "上传照片失败!");
return INPUT;
}
finally{
try{
if(null!=bis)bis.close();
}
catch(IOException e){
e.printStackTrace();
}
try{
if(null!=bos)bos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
return SUCCESS;
}
}
二、拿来孙鑫的 Struts2 深入详解,抄了代码 运行时没报错,可是查看上传文件的文件夹中没有发现对应上传的文件,奇怪了! 我用的是MyEclipse7.0 。好奇怪啊。返回的页面上 fileFileName dir都有,不过fileContextType 显示不出来.请高手帮忙啊!!!!具体代码:package tea;import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
public class LoadPicAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContextType;
private String uploadDir;
public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String getFileFileName() {
return fileFileName;
} public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
} public String getFileContextType() {
return fileContextType;
} public void setFileContextType(String fileContextType) {
this.fileContextType = fileContextType;
} public String getUploadDir() {
return uploadDir;
} public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
} public void validate() {
if(fileFileName==null || fileFileName.equals("")){
this.addFieldError("form1", "请选择上传照片!");
}
}
public String execute() throws Exception {
this.setUploadDir("/tphoto");
String path=ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir=new File(path);
if(!dir.exists())dir.mkdir();
BufferedOutputStream bos=null;
BufferedInputStream bis=null;
try{
FileInputStream fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream(new File(dir,this.getFileFileName()));
bos=new BufferedOutputStream(fos);
byte[] buf=new byte[4096];
int len=-1;
while((len=bis.read(buf))!=-1){
bos.write(buf, 0, len);
}
}
catch (IOException e) {
this.addFieldError("file", "上传照片失败!");
return INPUT;
}
finally{
try{
if(null!=bis)bis.close();
}
catch(IOException e){
e.printStackTrace();
}
try{
if(null!=bos)bos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
return SUCCESS;
}
}
解决方案 »
- java web 前段 IE bug
- bean实例化失败
- |javamy|Java对泛型在hibernate的Get是怎么写,总感觉不太对,有C#代码对比 谢谢
- Servlet+Javabean+MVC的简单语句
- 为什么log4j.xml已指定将日志输出到控制台与文件,但文件中没有日志,控制台的日志是正常,请高手指点?
- 怎么实现session能送到在不同服务中
- 用什么方法可以把double类型变量的d=4.93939399四舍五入转换成4.93 后边时两位小数 谢谢
- jboss无法启动
- 如何在JTable增加一行之后使该行的某个单元处于编辑状态? 急
- 英文版操作系统下用Jbuilder时不能显示中文,一输入中文就变成方格,怎么办?
- 【急】spring管理hibernate的一个问题
- hibernate qbc 时间段的查询
<filter-name>struts-clearup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-clearup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在web.xml中这个配置了没有
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UpLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String title;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath; public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} @SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
} public void setSavePath(String savePath) {
this.savePath = savePath;
} @Override
public String execute() throws Exception {
try{
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
BufferedInputStream bff = new BufferedInputStream(fis);
byte[] buffer = new byte[1024*4];
int len = 0;
while ((len = bff.read(buffer)) > 0) {
System.out.println("len = "+len);
fos.write(buffer, 0, len);
}
}catch(IOException e){
e.printStackTrace();
} return SUCCESS;
}}
<action name="upload" class="com.action.UpLoadAction">
//上传的类型
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpg,image/pjpeg
</param>
<param name="maximumSize">1024000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
//上传的路径
<param name="savePath">/upload</param>
<result>/uploadResult.jsp</result>
<result name="input">/error.jsp</result>
</action>
因为用的是MyEclipse的内置tomcat,所以文件被上传在tomcat的那个地方了,也不知道你是否和我的相同,试试看吧