jsp如何显示上传到Tomcat中的图片
这是上传Action的execute方法publicStringexecute()throwsException{StringrealPath=ServletAction...
这是上传Action的execute方法
public String execute() throws Exception{
String realPath = ServletActionContext.getRequest().getRealPath("/upload");
String targetDirectory = realPath;
if (fileName==null || "".equals(fileName.trim())){
this.addFieldError("doc", "文件不能为空");
}else {
targetFileName = generateFileName(fileName);
setDir(targetDirectory+"\\" + targetFileName);
File target = new File(targetDirectory,targetFileName);
try{
FileUtils.copyFile(doc, target);
} catch (Exception e){
e.printStackTrace();
}
HttpServletRequest request=ServletActionContext.getRequest();
String fId = request.getParameter("folderId");
int folderId = Integer.valueOf(fId);
//System.out.println(targetFileName);
//System.out.println("folderId="+folderId);
int photoId = photoDao.savePhoto(targetFileName,folderId);
//System.out.println("meiguo");
}
return SUCCESS;
}
private String generateFileName(String fileName){
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
String newfileName = formatDate+random + extension;
return newfileName;
}
} 展开
public String execute() throws Exception{
String realPath = ServletActionContext.getRequest().getRealPath("/upload");
String targetDirectory = realPath;
if (fileName==null || "".equals(fileName.trim())){
this.addFieldError("doc", "文件不能为空");
}else {
targetFileName = generateFileName(fileName);
setDir(targetDirectory+"\\" + targetFileName);
File target = new File(targetDirectory,targetFileName);
try{
FileUtils.copyFile(doc, target);
} catch (Exception e){
e.printStackTrace();
}
HttpServletRequest request=ServletActionContext.getRequest();
String fId = request.getParameter("folderId");
int folderId = Integer.valueOf(fId);
//System.out.println(targetFileName);
//System.out.println("folderId="+folderId);
int photoId = photoDao.savePhoto(targetFileName,folderId);
//System.out.println("meiguo");
}
return SUCCESS;
}
private String generateFileName(String fileName){
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = format.format(new Date());
int random = new Random().nextInt(10000);
int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);
String newfileName = formatDate+random + extension;
return newfileName;
}
} 展开
1个回答
2014-02-20
展开全部
你文件上传上去,肯定能拿到路径啊,直接把路径贴到你jsp中的img标签里面。
追问
我这个Action只是照书上写的 不太会Struts 从这个Action中 你能看出路径存在哪里了吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询