jsp通过controller获取图片

87 阅读1分钟
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ImageController {

    @GetMapping("/getImage")
    @ResponseBody
    public byte[] getImage(@RequestParam("imageName") String imageName) throws IOException {
        // 从文件系统中读取图片
        String imagePath = "C:/path/to/external/" + imageName; // 请替换为实际的图片路径
        File imageFile = new File(imagePath);
        return Files.readAllBytes(imageFile.toPath());
    }
}

检查和配置会话管理

确保其他请求的会话管理配置正确,避免会话冲突。例如,可以在 spring-mvc.xmlapplicationContext.xml 中配置会话管理:

xml

<beans:bean id="sessionManagementFilter" class="org.springframework.web.filter.DelegatingFilterProxy">
    <beans:property name="targetBeanName" value="springSessionRepositoryFilter" />
</beans:bean>

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>