Spring4下载f.txt文件解决方法

本文介绍了一个关于Spring框架中文件下载的问题及解决方案。当使用Spring 4.2.2.RELEASE版本时,通过浏览器访问带有@ResponseBody注解的Controller方法会触发文件下载而非预期的页面显示。文中提到该问题的原因在于响应头中出现了Content-Disposition:attachment;filename=f.txt,这导致了浏览器将内容视为附件处理。升级到Spring 4.2.5.RELEASE版本可以解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Spring 4.2.5.RELEASE版本就可以解决。
我之前使用Spring 4.2.2.RELEASE,通过浏览器访问Controller里返回信息有@ResponseBody的方法时,会下载f.txt文件。网上搜到的答案大多数是原因:

不正常是因为多了:Content-Disposition: attachment;filename=f.txt

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment;filename=f.txt
Content-Type: text/html;charset=UTF-8
Content-Length: 12
FileUtil.copyContent(srcDir, destDir, true); 报下列错误 n.hutool.core.io.IORuntimeException: Src is a directory but dest is a file! at cn.hutool.core.io.file.FileCopier.copy(FileCopier.java:185) at cn.hutool.core.io.FileUtil.copyContent(FileUtil.java:947) at com.cpic.tcx.service.service.impl.TcxCultureServicelmpl.sendMaterials(TcxCultureServicelmpl.java:695) at com.cpic.tcx.service.service.impl.TcxCultureServicelmpl.getMyCultureByld(TcxCultureServicelmpl.java:398) at com.cpic.tcx.service.service.impl.TcxCultureServicelmpl$$FastClassBySpringCGLIB$$be2a98b3.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) t com.cpic.tcx.service.service.impl.TcxCultureServicelmpI$$EnhancerBySpringCGLIB$$1ae56e60.getMyCultureByld(<generated>) at com.cpic.tcx.controller.TcxCultureController.getMyCultureByld(TcxCultureController.java:188) at com.cpic.tcx.controller.TcxCultureController$$FastClassBySpringCGLIB$$f69b79d4.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) at ora.sprinaframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.iava:163) nsh ua i st
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值