/s:form
Struts.xml
image/jpeg,image/x-png,image/gif,image/bmp
300000
/upload<!-- 该path与UploadAction中的path属性一致,并建了一个upload文件夹 -->
/uploadSuccess.jsp
/upload.jsp
UploadAction.java
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1222222222222L;
private File image;
private String imageContentType;
private String imageFileName;
private String path;//接受依赖注入的方法,该path在Struts.xml中配置
public void setPath(String value){
this.path = value;
}
public String getPath(){//返回文件上传后保存的位置
return ServletActionContext.getServletContext().getRealPath(path);
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
//生成一个新的文件名,以免上传相同文件名的文件
@SuppressWarnings("unused")
private String getFileName(String fileName){
int position = fileName.lastIndexOf(".");
String newname = fileName.substring(position);
return System.currentTimeMillis() + newname;
}
public String execute() throws Exception{
imageFileName = getFileName(imageFileName);
FileInputStream fis = new FileInputStream(image);
FileOutputStream fos = new FileOutputStream(getPath() + "\" + imageFileName);
byte [] b = new byte[4096];
int length = 0;
while((length = fis.read(b)) > 0){
fos.write(b, 0, length);
}
fis.close();
fos.close();
return SUCCESS;
}
}
WebRoot下我建立了个upload文件夹
上传成功后的现实页面,"/>
也成功的现实出来了图片和文件名
但是为什么upload文件夹没有该文件呢?
该文件是传到了tomcat的upload文件夹下面去了,我想要的结果是上传到项目的我建立的upload文件夹下,怎么办呀