java.io.FileNotFoundException: c:\imagestimg[2].jpg (拒绝访问。)

百度一下,知道是路径出了问题,找了好久原来是在拼接地址的时候忘记加**/**了
错误:String filePath = “c:/images”+name;
正确:String filePath = “c:/images/”+name;

这是构建一个简单的

文件上传

:
jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>实现文件长传</h1>
	<!--enctype="开启多媒体标签"  -->
	<form action="http://localhost/file" method="post" 
	enctype="multipart/form-data">
		<input name="fileImage" type="file" />
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

控制层的代码如下:

@RestController
public class FileController {
	@Autowired
	private FileService fileService;
	/**
	 * MultipartFile:专门处理文件上传的
	 * @param fileImage
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/file")
	public  String file( MultipartFile fileImage) throws IllegalStateException, IOException {
		//1.准备文件目录
		File fileDir = new File("c:/images");
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}
		//2.获取文件的名称
		String name = fileImage.getOriginalFilename(); 
		//3.准备文件的路径
		String filePath = "c:/images/"+name;
		//4.实现文件的上传
		fileImage.transferTo(new File(filePath));
		System.out.println("上传成功!!!!!!!!!!!!");
		
		return "文件上传成功@_@";
		
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值