我的项目使用Struts2,使用了Struts2自带的文件上传拦截器开发图片上传的功能。可以成功上传图片,上传成功后转发给JSP却无法显示图片(报404错误),而实际上图片的访问路径是正确的,因为将图片的访问路径复制粘贴到浏览器地址栏可以访问到图片并且显示出来。
看截图:
希望大佬,帮忙看看,代码我全部粘贴出来了,代码其实很简单,但是这个问题太奇葩了,图片地址完全正常,但是报404,而复制粘贴到地址栏去访问,又可以正常显示图片。我相当费解呀。
struts.xml的配置代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true"/>
<package name="interceptor" extends="struts-default" namespace="">
<!-- http://localhost:8080/si/uploadform.action -->
<action name="uploadform">
<result name="success">
/WEB-INF/jsp/uploadform.jsp
</result>
</action>
<action name="upload" class="priv.lwx.struts2.web.UploadAction">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="success">
/WEB-INF/jsp/uploadimage.jsp
</result>
</action>
</package>
</struts>
Action的代码如下:
package priv.lwx.struts2.web;
import org.apache.commons.io.IOUtils;
import java.io.*;
/**
* description
*
* @author liaowenxiong
* @date 2022/5/13 15:02
*/
public class UploadAction extends BaseAction {
private File some;
private String someFileName;
private String someContentType;
private String imagePath;
public String execute() throws IOException {
System.out.println("缓存文件路径:" + some);
System.out.println("源文件名:" + someFileName);
System.out.println("文件类型:" + someContentType);
// 生成随机的文件名
String imageName = "file_" + System.currentTimeMillis() + someFileName.substring(someFileName.lastIndexOf("."));
System.out.println("随机文件名:" + imageName);
// 这个值在JSP页面中链接到图片时会使用到
imagePath = "upload-image/" + imageName;
// 获取目录upload-image的绝对路径
String uploadPath = application.getRealPath("upload-image");
// 判断目录upload-image是否存在,否则创建该目录
File file = new File(uploadPath);
if (!file.exists()) {
file.mkdirs();
}
// 获取文件在服务器主机中的绝对路径
String realPath = application.getRealPath(imagePath);
System.out.println("文件的绝对路径:" + realPath);
// 从缓存中读取图片
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(some));
// 构造指向目标文件的字节输出流对象,用来向目标文件写入数据
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(realPath));
// 写入数据到目标文件中
IOUtils.copy(bis, bos);
bis.close();
bos.close();
return "success";
}
public File getSome() {
return some;
}
public void setSome(File some) {
this.some = some;
}
public String getSomeFileName() {
return someFileName;
}
public void setSomeFileName(String someFileName) {
this.someFileName = someFileName;
}
public String getSomeContentType() {
return someContentType;
}
public void setSomeContentType(String someContentType) {
this.someContentType = someContentType;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
uploadform.jsp代码如下:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>
文件上传
</h1>
<s:form action="upload" method="post" theme="xhtml" enctype="multipart/form-data">
<s:file label="File" name="some"/>
<s:submit value="提交"/>
</s:form>
<br/>
<br/>
<br/>
</body>
</html>
uploadimage.jsp代码如下:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>
文件上传成功
</h1>
<img src="${imagePath}"/>
<br/>
<br/>
<br/>
</body>
</html>
在index.html中加入meta标签 <meta name="referrer" content="no-referrer"> 试试