直接上代码
java代码 编写方法获取文件信息并存储数据库:
/**
* 增加平台
* @param Name 平台名称
* @param Url 平台Url
* @param items_pic 图片路径
* @param httpServletResponse
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("IntranetInsert")
@ResponseBody
public String IntranetInsert(@RequestParam("sname") String Name,@RequestParam("surl") String Url,@RequestParam(required = false) MultipartFile items_pic,HttpServletResponse httpServletResponse) throws IllegalStateException, IOException {
IntranetSystem intranetSystem = new IntranetSystem();
if(items_pic != null) {
//存储图片的物理路径
String pic_path = "D:\\apqixunimulu\\";
//图片原始名称
String originalFilename = items_pic.getOriginalFilename();
//新的图片名称
String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//新图片
File newFile = new File(pic_path + newFileName);
//将内存中的数据写入磁盘
items_pic.transferTo(newFile);
//存入数据库
if(newFileName != "") {
intranetSystem.setSystemlogo(newFileName);
}else {
return null;
}
System.out.println(newFileName);
}
Date date = new Date();
String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
if(dateStr != "" && Name != "" && Url != "") {
intranetSystem.setCreatetime(dateStr);
intranetSystem.setSystemname(Name);
intranetSystem.setSystemurl(Url);
}else {
return null;
}
Integer num = intranetSystemService.IntranetInsert(intranetSystem);
System.out.println(dateStr);
System.out.println(Name);
System.out.println(Url);
System.out.println("文件长度: " + items_pic.getSize());
System.out.println("文件类型: " + items_pic.getContentType());
System.out.println("文件名称: " + items_pic.getName());
return "showobj/IntranetHomePage";
}
前端代码也贴上:
此前端代码是我写的一个添加框 没有css样式,仅供参考 可以自己写一下 添加框
<div class="tanBtn none" id="tanBtn1">
<div class="tanTitle">
<p>Create a new logo</p>
<a href="JavaScript:void(0)"><img src="resources/pc/showobj/images/Intranetquxiao.png" class="quxiao" onclick="dianji()"></a>
</div>
<div class="tanConten">
<iframe src="" frameborder="0" name="iframeContent" style="display: none;"></iframe>
<form id="from" method="post" action="IntranetSystem/IntranetInsert" target="iframeContent" enctype="multipart/form-data">
<div class="txt-fld">
<label for="">项目名</label>
<input id="sname" class="good_input" name="sname" type="text">
</div>
<div class="txt-fld">
<label for="">项目Url</label>
<input name="surl" type="text" id="surl">
</div>
<div class="txt-fld_c">
<label for="">平台logo</label>
<a href="JavaScript:void(0)" class="file">选择文件
<input type="file" value="上传图片" id="picture" name="items_pic"></a>
<img src="" class="none">
</div>
<div class="btn-fld">
<button type="submit" id="tijiao">提交文本 »</button>
</div>
</form>
</div>
</div>
js文件:
$(function(){
$('#tijiao').click(function(){
var imgPath = $("#picture").val();
var name = $("#sname").val()
var url = $("#surl").val()
//判断是否有选择上传文件
if (imgPath == "") {
alert("请选择上传图片!");
return;
}
//判断上传文件的后缀名
var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
if (strExtension != 'jpg' && strExtension != 'gif'&& strExtension != 'png' && strExtension != 'bmp') {
alert("请选择图片文件");
return;
}
if (name == "") {
alert("项目名不能为空");
return false;
}
if (url == "") {
alert("项目Url不能为空");
return false;
}
$("iframe[name=iframeContent]").on("load", function() {
var responseText = $("iframe")[0].contentDocument.body.getElementsByTagName("pre")[0].innerHTML;
window.location.reload();//页面刷新
})
});
$('.deltijiao').click(function(){
$("iframe[name=iframeContent]").on("load", function() {
var responseText = $("iframe")[0].contentDocument.body.getElementsByTagName("pre")[0].innerHTML;
window.location.reload();//页面刷新
})
});
});
最后配置下tomcat虚拟目录
- 使用java web进行文件上传下载时,若放在web项目路径下,则可以直接根据相对路径进行访问,但若文件较多、考虑到部署、更新、迁移和备份等问题,有时需要把文件的存储目录放在非项目目录下。
例如项目路径是D:/tomcat8/project,文件存放路径是E:/files/
,此时就需要配置虚拟路径映射来解决,否则在浏览器上访问E:/files/a.txt 是无法访问的。
1. 配置方法很简单,只需要在tomcat的conf目录下找到server.xml文件,在<Host></Host>标签之间加上下面这行配置即可
<Context path="/file" docBase="E:\files" debug="0" reloadable="true"/>
假设E:/files/下有一个文件 a.txt,则在页面上我们可以通过如下路径访问:<a href = '/file/a.txt'>文件</a>