栀子栀子栀 2015-04-15 04:38 采纳率: 0%
浏览 3406

Struts2的上传问题,图片怎么存在tomcat里而不是项目里呢?

jsp页面

/s:file
/s:submit

/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文件夹下,怎么办呀

  • 写回答

8条回答 默认 最新

  • devmiao 2015-04-15 04:45
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器