文件下载:
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping("download.do") public ResponseEntity<byte[]> download() throws IOException { File file=new File("F://Y2//OA//.metadata//.me_tcat//webapps//SpringMVC_fileupload//images//111.jpg"); HttpHeaders headers = new HttpHeaders(); String fileName=new String("你好.jpg".getBytes("utf-8"),"iso-8859-1");//为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
<form action="${pageContext.request.contextPath}/frist.do" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="uploadFile"/><br/> <input type="submit" value="上传"/> </form> <a href="${pageContext.request.contextPath}/download.do?111.jpg">下载</a> 注解版配置欢迎你的使用~~~
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16079.html