Java 虚拟路径与文件上传

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




配置虚拟路径可以让多个项目使用共同文件夹,达到资源共用的目的

高版本的myeclipse自带tomcat,在java工作区/Servers/MyEclipse Tomcat v8.5-config/

在host标签内加入

 <Context path="/upload" docBase="F:/javawebfile" reloadable="false" />

img标签显示

<img src='/upload/20190818171921-hyd.jpg' width="146" height="148">

文件上传servlet:https://www.idaobin.com/archives/2068.html

文件上传spring

	/**
	 * 日期-文件名
	 * @return
	 */
	private String getDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String dt=df.format(new Date());
		return dt;
	}

	@RequestMapping(value = "/fileUpload")
	@ResponseBody
	public String fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException {
		long startTime = System.currentTimeMillis();
		System.out.println("fileName:" + file.getOriginalFilename());
		//String path = getServletContext().getRealPath("/"); + file.getOriginalFilename();
		String path = "f:/" + getDate()+ "-" + file.getOriginalFilename();
		System.out.println(path);
		File newFile = new File(path);
		// 通过CommonsMultipartFile的方法直接写文件(注意这个时候)
		file.transferTo(newFile);
		long endTime = System.currentTimeMillis();
		System.out.println("方法二的运行时间:" + String.valueOf(endTime - startTime) + "ms");
		return "success";
	}

xml配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

前端html代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript"
	src="https://www.idaobin.com/js/jquery-3.2.1.js"></script>
<script type="text/javascript"
	src="https://www.idaobin.com/test/ajaxfileupload.js"></script>
<script type="text/javascript">
	function ajaxFileUpload() {
		$.ajaxFileUpload(
			{
				url : 'fileUpload', //用于文件上传的服务器端请求地址
				secureuri : false, //一般设置为false
				fileElementId : 'file', //文件上传空间的id属性 <input type="file" id="file" name="file" />
				dataType : 'text', //返回值类型 一般设置为json
				success : function(data, status) //服务器成功响应处理函数
				{
					//方法一,用后台返回值
					alert(data); //后台out输出的值

				},
				error : function(data, status, e) //服务器响应失败处理函数
				{
					//方法一,用后台返回值
					alert(data); //后台out输出的值

				}
			}
		)

		return false;

	}
</script>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<!-- form表单提交方式 -->
	<form name="Form2" action="fileUpload" method="post"
		enctype="multipart/form-data">
		<input type="file" name="file"> <input type="submit"
			value="upload" />
	</form>
	
	<!-- ajax提交方式 -->
	<input type="file" id="file" name="file" />
	<br />
	<input type="button" value="上传" onclick="return ajaxFileUpload();">

	<br>
	<p>
		<span id="myspan"></span> <img id="sctx"
			src='/upload/20190818171921-hyd.jpg' width="146" height="148">
	</p>
</body>
</html>

注意:

1.文件上传路径要与虚拟路径一致

2.过滤器把图片文件过滤掉导致404

原文链接:https://www.idaobin.com/archives/2102.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

82 − 72 =