国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java大牛養(yǎng)成記——圖片上傳

2019-11-06 07:39:50
字體:
供稿:網(wǎng)友

背景:感覺過了好久沒有好好的寫文章了,今天休息就來整理一下最近接觸到的內(nèi)容吧。順便梳理一下學(xué)習(xí)到的知識。今天需要梳理的內(nèi)容是圖片的上傳。

一、前期準(zhǔn)備

在配置文件中配置你要上傳的路徑:

picture_windows=E/://picture//picture_linux=/home/picture/

二、jsp

<%@ page contentType="text/html;charset=UTF-8" %><html><head>	<title>上傳圖片</title>	<meta name="decorator" content="default"/>	<script type="text/javascript">		$(document).ready(function() {			$("#name").focus();			$("#inputForm").validate({				submitHandler: function(form){					loading('正在提交,請稍等...');					form.submit();				},				errorContainer: "#messageBox",				errorPlacement: function(error, element) {					$("#messageBox").text("輸入有誤,請先更正。");					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){						error.appendTo(element.parent().parent());					} else {						error.insertAfter(element);					}				}			});		});			</script></head><body>			<form:form id="inputForm" modelAttribute="picture" action="${pageContext.request.contextPath}/sys/picture/save" method="post" class="form-horizontal" enctype="multipart/form-data">			<div class="control-group">			<label class="control-label">上傳圖片:</label>			<div class="controls">				<input type="file" name="file" accept="image/png,image/jpeg" class="required" value="file"/>				<span class="help-inline"><font color="red">*只支持JPG、PNG格式文件</font> </span>			</div>		</div>					<div class="form-actions">			<input id="btnSubmit" class="btn btn-PRimary" type="submit" value="保 存"/>			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/> 		</div>	</form:form>	</body></html>

三、controller

/** * 上傳圖片Controller * @author 麗杰 * @version 2017-03-04 * */@Controller@RequestMapping(value = "${adminPath}/sys/picture")public class PictureController{  private File file;    public File getFile() {  return file;  }  public void setFile(File file) {    this.file = file;  }    //-----------保存+文件上傳-------start--------  @RequestMapping(value = "save")  public String save(@RequestParam("file") CommonsMultipartFile file,PictureEntity pictureEntity, Model model, RedirectAttributes redirectAttributes)throws IOException {        //1、生成文件的id    String fileId = UUID.randomUUID().toString().replaceAll("-", "");        //2、獲得文件的真實(shí)名字    String fileName=file.getOriginalFilename();        //3、獲取文件的后綴    String suffix=fileName.substring(fileName.lastIndexOf(".")+1);        //4、拼接文件名:ID+"."+后綴    String jointName = fileId+"."+suffix;        //5、判斷系統(tǒng)的類型并找到圖片文件存放的位置    String dir =null;    if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){      dir=Global.getConfig("picture_windows");    }    if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){      dir=Global.getConfig("picture_linux");    }        //6、判斷picture文件夾是否存在,不存在,則創(chuàng)建    File dirFile=new File(dir);    if(!dirFile.exists()){      dirFile.mkdirs();    }        //7、獲得上傳路徑以及拼接上傳文件名    String path=dir+jointName;        //8、上傳    File newFile=new File(path);    file.transferTo(newFile);        addMessage(redirectAttributes, "保存圖片成功!");    return "redirect:" + adminPath + "/sys/picture/";  }  //-----------保存+文件上傳-------end--------}

四、學(xué)習(xí)心得

1、從配置文件中讀取路徑其實(shí)用Global.getConfig就可以了。

2、input的accept="image/png,image/jpeg" 屬性可以控制上傳文件的類型。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邢台市| 金堂县| 竹溪县| 东乡族自治县| 禹州市| 香格里拉县| 安西县| 铜山县| 镇巴县| 嘉定区| 禹州市| 柘荣县| 新巴尔虎左旗| 宜昌市| 景谷| 高青县| 吉隆县| 开阳县| 陵水| 高唐县| 临猗县| 正阳县| 布尔津县| 宁安市| 奉新县| 济源市| 高密市| 枣阳市| 安宁市| 余江县| 哈巴河县| 太湖县| 康平县| 榆树市| 商洛市| 寻乌县| 湖口县| 黔西县| 隆子县| 勐海县| 正宁县|