SpringMvc + Jsp+ 富文本 kindeditor 進行 圖片ftp上傳nginx服務器 實現


一:html 原生態的附件上傳

image

image

二:實現邏輯分析;

1.1.1 需求分析

Common.js

1、綁定事件

clip_image002

2、初始化參數

clip_image004

3、上傳圖片的url:

/pic/upload

4、上圖片參數名稱:

uploadFile

5、返回結果數據類型json

參考文檔:

http://kindeditor.net/docs/upload.html

返回格式(JSON)

  1 //成功時
  2 
  3 {
  4 
  5 "error" : 0,
  6 
  7 "url" : "http://www.example.com/path/to/file.ext"
  8 
  9 }
 10 
 11 //失敗時
 12 
 13 {
 14 
 15 "error" : 1,
 16 
 17 "message" : "錯誤信息"
 18 
 19 }
 20 
6  Service

image

 

image

7:  Controller

image

8:需要引入的jar包的maven 配置參數:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3 
  4 	<artifactId>taotao-common</artifactId>
  5 	<!-- jar包依賴 -->
  6 	<dependencies>
  7 		<!-- 時間操作組件 -->
  8 		<dependency>
  9 			<groupId>joda-time</groupId>
 10 			<artifactId>joda-time</artifactId>
 11 		</dependency>
 12 		<!-- Apache工具組件 -->
 13 
 14 		<dependency>
 15 			<groupId>org.apache.commons</groupId>
 16 			<artifactId>commons-io</artifactId>
 17 		</dependency>
 18 		<dependency>
 19 			<groupId>commons-net</groupId>
 20 			<artifactId>commons-net</artifactId>
 21 		</dependency>
 22 		<!-- Jackson Json處理工具包 -->
 23 		<dependency>
 24 			<groupId>com.fasterxml.jackson.core</groupId>
 25 			<artifactId>jackson-databind</artifactId>
 26 		</dependency>
 27 	        <!-- 文件上傳組件 -->
 28 		<dependency>
 29 			<groupId>commons-fileupload</groupId>
 30 			<artifactId>commons-fileupload</artifactId>
 31 		</dependency>
 32 
 33 	</dependencies>
 34 
 35 
 36 </project>

 

三:邏輯實現:

1:前端實現:

 

  1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2 <link href="/js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
  3 <script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
  4 <script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/lang/zh_CN.js"></script>
  5 <div style="padding:10px 10px 10px 10px">
  6 	<form id="itemAddForm" class="itemForm" method="post">
  7 	    <table cellpadding="5">
  8 	        <tr>
  9 	            <td>商品類目:</td>
 10 	            <td>
 11 	            	<a href="javascript:void(0)" class="easyui-linkbutton selectItemCat">選擇類目</a>
 12 	            	<input type="hidden" name="cid" style="width: 280px;"></input>
 13 	            </td>
 14 	        </tr>
 15 	        <tr>
 16 	            <td>商品標題:</td>
 17 	            <td><input class="easyui-textbox" type="text" name="title" data-options="required:true" style="width: 280px;"></input></td>
 18 	        </tr>
 19 	        <tr>
 20 	            <td>商品賣點:</td>
 21 	            <td><input class="easyui-textbox" name="sellPoint" data-options="multiline:true,validType:'length[0,150]'" style="height:60px;width: 280px;"></input></td>
 22 	        </tr>
 23 	        <tr>
 24 	            <td>商品價格:</td>
 25 	            <td><input class="easyui-numberbox" type="text" name="priceView" data-options="min:1,max:99999999,precision:2,required:true" />
 26 	            	<input type="hidden" name="price"/>
 27 	            </td>
 28 	        </tr>
 29 	        <tr>
 30 	            <td>庫存數量:</td>
 31 	            <td><input class="easyui-numberbox" type="text" name="num" data-options="min:1,max:99999999,precision:0,required:true" /></td>
 32 	        </tr>
 33 	        <tr>
 34 	            <td>條形碼:</td>
 35 	            <td>
 36 	                <input class="easyui-textbox" type="text" name="barcode" data-options="validType:'length[1,30]'" />
 37 	            </td>
 38 	        </tr>
 39 	        <tr>
 40 	            <td>商品圖片:</td>
 41 	            <td>
 42 	            	 <a href="javascript:void(0)" class="easyui-linkbutton picFileUpload">上傳圖片</a>
 43 	                 <input type="hidden" name="image"/>
 44 	            </td>
 45 	        </tr>
 46 	        <tr>
 47 	            <td>商品描述:</td>
 48 	            <td>
 49 	                <textarea style="width:800px;height:300px;visibility:hidden;" name="desc"></textarea>
 50 	            </td>
 51 	        </tr>
 52 	        <tr class="params hide">
 53 	        	<td>商品規格:</td>
 54 	        	<td>
 55 
 56 	        	</td>
 57 	        </tr>
 58 	    </table>
 59 	    <input type="hidden" name="itemParams"/>
 60 	</form>
 61 	<div style="padding:5px">
 62 	    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提交</a>
 63 	    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()">重置</a>
 64 	</div>
 65 </div>
 66 <script type="text/javascript">
 67 	var itemAddEditor ;
 68 	//頁面初始化完畢后執行此方法
 69 	$(function(){
 70 		//創建富文本編輯器
 71 		itemAddEditor = TAOTAO.createEditor("#itemAddForm [name=desc]");
 72 		//初始化類目選擇和圖片上傳器
 73 		TAOTAO.init({fun:function(node){
 74 			//根據商品的分類id取商品 的規格模板,生成規格信息。第四天內容。
 75 			//TAOTAO.changeItemParam(node, "itemAddForm");
 76 		}});
 77 	});
 78 	//提交表單
 79 	function submitForm(){
 80 		//有效性驗證
 81 		if(!$('#itemAddForm').form('validate')){
 82 			$.messager.alert('提示','表單還未填寫完成!');
 83 			return ;
 84 		}
 85 		//取商品價格,單位為“分”
 86 		$("#itemAddForm [name=price]").val(eval($("#itemAddForm [name=priceView]").val()) * 100);
 87 		//同步文本框中的商品描述
 88 		itemAddEditor.sync();
 89 		//取商品的規格
 90 		/*
 91 		var paramJson = [];
 92 		$("#itemAddForm .params li").each(function(i,e){
 93 			var trs = $(e).find("tr");
 94 			var group = trs.eq(0).text();
 95 			var ps = [];
 96 			for(var i = 1;i<trs.length;i++){
 97 				var tr = trs.eq(i);
 98 				ps.push({
 99 					"k" : $.trim(tr.find("td").eq(0).find("span").text()),
100 					"v" : $.trim(tr.find("input").val())
101 				});
102 			}
103 			paramJson.push({
104 				"group" : group,
105 				"params": ps
106 			});
107 		});
108 		//把json對象轉換成字符串
109 		paramJson = JSON.stringify(paramJson);
110 		$("#itemAddForm [name=itemParams]").val(paramJson);
111 		*/
112 		//ajax的post方式提交表單
113 		//$("#itemAddForm").serialize()將表單序列號為key-value形式的字符串
114 		$.post("/item/save",$("#itemAddForm").serialize(), function(data){
115 			if(data.status == 200){
116 				$.messager.alert('提示','新增商品成功!');
117 			}
118 		});
119 	}
120 
121 	function clearForm(){
122 		$('#itemAddForm').form('reset');
123 		itemAddEditor.html('');
124 	}
125 </script>
126 

commom.js

  1 Date.prototype.format = function(format){
  2     var o =  {
  3     "M+" : this.getMonth()+1, //month 
  4     "d+" : this.getDate(), //day 
  5     "h+" : this.getHours(), //hour 
  6     "m+" : this.getMinutes(), //minute 
  7     "s+" : this.getSeconds(), //second 
  8     "q+" : Math.floor((this.getMonth()+3)/3), //quarter 
  9     "S" : this.getMilliseconds() //millisecond 
 10     };
 11     if(/(y+)/.test(format)){
 12     	format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
 13     }
 14     for(var k in o)  {
 15 	    if(new RegExp("("+ k +")").test(format)){
 16 	    	format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
 17 	    }
 18     }
 19     return format;
 20 };
 21 
 22 var TT = TAOTAO = {
 23 	// 編輯器參數
 24 	kingEditorParams : {
 25 		//指定上傳文件參數名稱
 26 		filePostName  : "uploadFile",
 27 		//指定上傳文件請求的url。
 28 		uploadJson : '/pic/upload',
 29 		//上傳類型,分別為image、flash、media、file
 30 		dir : "image"
 31 	},
 32 	// 格式化時間
 33 	formatDateTime : function(val,row){
 34 		var now = new Date(val);
 35     	return now.format("yyyy-MM-dd hh:mm:ss");
 36 	},
 37 	// 格式化連接
 38 	formatUrl : function(val,row){
 39 		if(val){
 40 			return "<a href='"+val+"' target='_blank'>查看</a>";
 41 		}
 42 		return "";
 43 	},
 44 	// 格式化價格
 45 	formatPrice : function(val,row){
 46 		return (val/1000).toFixed(2);
 47 	},
 48 	// 格式化商品的狀態
 49 	formatItemStatus : function formatStatus(val,row){
 50         if (val == 1){
 51             return '正常';
 52         } else if(val == 2){
 53         	return '<span style="color:red;">下架</span>';
 54         } else {
 55         	return '未知';
 56         }
 57     },
 58 
 59     init : function(data){
 60     	// 初始化圖片上傳組件
 61     	this.initPicUpload(data);
 62     	// 初始化選擇類目組件
 63     	this.initItemCat(data);
 64     },
 65     // 初始化圖片上傳組件
 66     initPicUpload : function(data){
 67     	$(".picFileUpload").each(function(i,e){
 68     		var _ele = $(e);
 69     		_ele.siblings("div.pics").remove();
 70     		_ele.after('\
 71     			<div class="pics">\
 72         			<ul></ul>\
 73         		</div>');
 74     		// 回顯圖片
 75         	if(data && data.pics){
 76         		var imgs = data.pics.split(",");
 77         		for(var i in imgs){
 78         			if($.trim(imgs[i]).length > 0){
 79         				_ele.siblings(".pics").find("ul").append("<li><a href='"+imgs[i]+"' target='_blank'><img src='"+imgs[i]+"' width='80' height='50' /></a></li>");
 80         			}
 81         		}
 82         	}
 83         	//給“上傳圖片按鈕”綁定click事件
 84         	$(e).click(function(){
 85         		var form = $(this).parentsUntil("form").parent("form");
 86         		//打開圖片上傳窗口
 87         		KindEditor.editor(TT.kingEditorParams).loadPlugin('multiimage',function(){
 88         			var editor = this;
 89         			editor.plugin.multiImageDialog({
 90 						clickFn : function(urlList) {
 91 							var imgArray = [];
 92 							KindEditor.each(urlList, function(i, data) {
 93 								imgArray.push(data.url);
 94 								form.find(".pics ul").append("<li><a href='"+data.url+"' target='_blank'><img src='"+data.url+"' width='80' height='50' /></a></li>");
 95 							});
 96 							form.find("[name=image]").val(imgArray.join(","));
 97 							editor.hideDialog();
 98 						}
 99 					});
100         		});
101         	});
102     	});
103     },
104 
105     // 初始化選擇類目組件
106     initItemCat : function(data){
107     	$(".selectItemCat").each(function(i,e){
108     		var _ele = $(e);
109     		if(data && data.cid){
110     			_ele.after("<span style='margin-left:10px;'>"+data.cid+"</span>");
111     		}else{
112     			_ele.after("<span style='margin-left:10px;'></span>");
113     		}
114     		_ele.unbind('click').click(function(){
115     			$("<div>").css({padding:"5px"}).html("<ul>")
116     			.window({
117     				width:'500',
118     			    height:"450",
119     			    modal:true,
120     			    closed:true,
121     			    iconCls:'icon-save',
122     			    title:'選擇類目',
123     			    onOpen : function(){
124     			    	var _win = this;
125     			    	$("ul",_win).tree({
126     			    		url:'/item/cat/list',
127     			    		animate:true,
128     			    		onClick : function(node){
129     			    			if($(this).tree("isLeaf",node.target)){
130     			    				// 填寫到cid中
131     			    				_ele.parent().find("[name=cid]").val(node.id);
132     			    				_ele.next().text(node.text).attr("cid",node.id);
133     			    				$(_win).window('close');
134     			    				if(data && data.fun){
135     			    					data.fun.call(this,node);
136     			    				}
137     			    			}
138     			    		}
139     			    	});
140     			    },
141     			    onClose : function(){
142     			    	$(this).window("destroy");
143     			    }
144     			}).window('open');
145     		});
146     	});
147     },
148 
149     createEditor : function(select){
150     	return KindEditor.create(select, TT.kingEditorParams);
151     },
152 
153     /**
154      * 創建一個窗口,關閉窗口后銷毀該窗口對象。<br/>
155      *
156      * 默認:<br/>
157      * width : 80% <br/>
158      * height : 80% <br/>
159      * title : (空字符串) <br/>
160      *
161      * 參數:<br/>
162      * width : <br/>
163      * height : <br/>
164      * title : <br/>
165      * url : 必填參數 <br/>
166      * onLoad : function 加載完窗口內容后執行<br/>
167      *
168      *
169      */
170     createWindow : function(params){
171     	$("<div>").css({padding:"5px"}).window({
172     		width : params.width?params.width:"80%",
173     		height : params.height?params.height:"80%",
174     		modal:true,
175     		title : params.title?params.title:"",
176     		href : params.url,
177 		    onClose : function(){
178 		    	$(this).window("destroy");
179 		    },
180 		    onLoad : function(){
181 		    	if(params.onLoad){
182 		    		params.onLoad.call(this);
183 		    	}
184 		    }
185     	}).window("open");
186     },
187 
188     closeCurrentWindow : function(){
189     	$(".panel-tool-close").click();
190     },
191 
192     changeItemParam : function(node,formId){
193     	$.getJSON("/item/param/query/itemcatid/" + node.id,function(data){
194 			  if(data.status == 200 && data.data){
195 				 $("#"+formId+" .params").show();
196 				 var paramData = JSON.parse(data.data.paramData);
197 				 var html = "<ul>";
198 				 for(var i in paramData){
199 					 var pd = paramData[i];
200 					 html+="<li><table>";
201 					 html+="<tr><td colspan=\"2\" class=\"group\">"+pd.group+"</td></tr>";
202 
203 					 for(var j in pd.params){
204 						 var ps = pd.params[j];
205 						 html+="<tr><td class=\"param\"><span>"+ps+"</span>: </td><td><input autocomplete=\"off\" type=\"text\"/></td></tr>";
206 					 }
207 
208 					 html+="</li></table>";
209 				 }
210 				 html+= "</ul>";
211 				 $("#"+formId+" .params td").eq(1).html(html);
212 			  }else{
213 				 $("#"+formId+" .params").hide();
214 				 $("#"+formId+" .params td").eq(1).empty();
215 			  }
216 		  });
217     },
218     getSelectionsIds : function (select){
219     	var list = $(select);
220     	var sels = list.datagrid("getSelections");
221     	var ids = [];
222     	for(var i in sels){
223     		ids.push(sels[i].id);
224     	}
225     	ids = ids.join(",");
226     	return ids;
227     },
228 
229     /**
230      * 初始化單圖片上傳組件 <br/>
231      * 選擇器為:.onePicUpload <br/>
232      * 上傳完成后會設置input內容以及在input后面追加<img>
233      */
234     initOnePicUpload : function(){
235     	$(".onePicUpload").click(function(){
236 			var _self = $(this);
237 			KindEditor.editor(TT.kingEditorParams).loadPlugin('image', function() {
238 				this.plugin.imageDialog({
239 					showRemote : false,
240 					clickFn : function(url, title, width, height, border, align) {
241 						var input = _self.siblings("input");
242 						input.parent().find("img").remove();
243 						input.val(url);
244 						input.after("<a href='"+url+"' target='_blank'><img src='"+url+"' width='80' height='50'/></a>");
245 						this.hideDialog();
246 					}
247 				});
248 			});
249 		});
250     }
251 };
252 

 

2:后台的邏輯實現

2-1: ftp配置文件 

  1 FTP_ADDRESS=192.168.1.5
  2 FTP_PORT=21
  3 FTP_USERNAME=ftpuser
  4 FTP_PASSWORD=123456
  5 FTP_BASEPATH=/home/ftpuser/www/images/
  6 IMAGE_BASE_URL=http://192.168.1.5/images/
  7 

引入到SpringMVC.xml配置文件中

image

 

2-2:SpringMvc.xml的實現配置

  1 
  2 	<!-- 定義文件上傳解析器 -->
  3 	<bean id="multipartResolver"
  4 		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  5 		<!-- 設定默認編碼 -->
  6 		<property name="defaultEncoding" value="UTF-8"></property>
  7 		<!-- 設定文件上傳的最大值5MB,5*1024*1024 -->
  8 		<property name="maxUploadSize" value="5242880"></property>
  9 	</bean>
 10 
 11 

2-3:controller層實現:

  1 package com.taotao.controller;
  2 
  3 import java.util.Map;
  4 
  5 import org.springframework.beans.factory.annotation.Autowired;
  6 import org.springframework.stereotype.Controller;
  7 import org.springframework.web.bind.annotation.RequestMapping;
  8 import org.springframework.web.bind.annotation.ResponseBody;
  9 import org.springframework.web.multipart.MultipartFile;
 10 
 11 import com.taotao.common.utils.JsonUtils;
 12 import com.taotao.service.PictureService;
 13 
 14 /**
 15  *
 16  * @ClassName:  PictureController
 17  * @Description:上傳圖片處理
 18  * @author:  劉軍/shall_liu(1136808529@qq.com)
 19  * @date:   2017年8月26日 下午4:03:42
 20  *
 21  * @Copyright: 2017
 22  */
 23 @Controller
 24 public class PictureController {
 25 
 26 	@Autowired
 27 	private PictureService  pictureService;
 28 
 29 	/**
 30 	 *
 31 	 * @Title: pictureUpload
 32 	 * @Description:  接收圖片上傳服務
 33 	 * @param: @param uploadFile
 34 	 * @param: @return
 35 	 * @return: Map
 36 	 * @throws
 37 	 */
 38 	@RequestMapping("/pic/upload")
 39 	@ResponseBody
 40 	public String pictureUpload(MultipartFile uploadFile){
 41 		Map result =pictureService.uploadPicture(uploadFile);
 42 		//為了保證能的兼容性,需要把  Result轉換成json格式的字符串
 43 	    String  json=	JsonUtils.objectToJson(result);
 44 		return json;
 45 	}
 46 
 47 
 48 
 49 
 50 }
 51 

2_-4: service 層實現

  1 package com.taotao.service;
  2 
  3 import java.util.Map;
  4 
  5 import org.springframework.web.multipart.MultipartFile;
  6 
  7 /**
 8  *
 9  * @ClassName:  PictureService
 10  * @Description: 圖片生成接口
 11  *
 12  * @author:  劉軍/shall_liu(1136808529@qq.com)
 13  * @date:   2017年8月26日 下午2:29:35
 14  *
 15  * @Copyright: 2017
 16  */
 17 public interface PictureService {
 18 
 19 	/**
 20 	 *
 21 	 * @Title: uploadPicture
 22 	 * @Description:  生成圖片接口
 23 	 * @param: @param uploadFile
 24 	 * @param: @return
 25 	 * @return: Map
 26 	 * @throws
 27 	 */
 28 	Map uploadPicture(MultipartFile uploadFile);
 29 
 30 
 31 
 32 }
 33 

 

  1 package com.taotao.service.impl;
  2 
  3 import java.io.IOException;
  4 import java.util.HashMap;
  5 import java.util.Map;
  6 
  7 import org.apache.ibatis.annotations.ResultMap;
  8 import org.joda.time.DateTime;
  9 import org.springframework.beans.factory.annotation.Value;
 10 import org.springframework.stereotype.Service;
 11 import org.springframework.web.multipart.MultipartFile;
 12 
 13 import com.mysql.jdbc.ResultSetMetaData;
 14 import com.taotao.common.utils.FtpUtil;
 15 import com.taotao.common.utils.IDUtils;
 16 import com.taotao.service.PictureService;
 17 /**
 18  *
 19  * @ClassName:  PictureServiceImpl
 20  * @Description:圖片上傳 服務
 21  * @author:  劉軍/shall_liu(1136808529@qq.com)
 22  * @date:   2017年8月26日 下午2:33:25
 23  *
 24  * @Copyright: 2017
 25  */
 26 @Service
 27 public class PictureServiceImpl  implements PictureService{
 28    /**
 29 	 * 實現原理:
 30 	 * 利用了springMvc的加載properties的一套實現工具的機制
 31 	 * 使用注解@value 自動注入properties的參數信息
 32 	 *
 33 	 * ftp ip 訪問地址
 34 	 */
 35 	@Value("${FTP_ADDRESS}")
 36 	private String FTP_ADDRESS;
 37 	/**
 38 	 * ftp 服務端口
 39 	 */
 40 	@Value("${FTP_PORT}")
 41 	private String FTP_PORT;
 42 	/**
 43 	 * ftp 登錄用戶名
 44 	 */
 45 	@Value("${FTP_USERNAME}")
 46 	private String FTP_USERNAME;
 47 	/**
 48 	 * ftp 登錄密碼
 49 	 */
 50 	@Value("${FTP_PASSWORD}")
 51 	private String FTP_PASSWORD;
 52 	/**
 53 	 * ftp 保存文件的跟文件路徑
 54 	 */
 55 	@Value("${FTP_BASEPATH}")
 56 	private String FTP_BASEPATH;
 57 	/**
 58 	 * 圖片服務器的基礎URL
 59 	 */
 60 	@Value("${IMAGE_BASE_URL}")
 61 	private String IMAGE_BASE_URL;
 62 
 63 
 64 
 65 	 /**
 66 	  *
 67 	  * <p>Title: uploadPicture</p>
 68 	  * <p>Description: </p>  圖片上傳的實現
 69 	  * @param uploadFile
 70 	  * @return
 71 	  * @see com.taotao.service.PictureService#uploadPicture(org.springframework.web.multipart.MultipartFile)
 72 	  */
 73 	@Override
 74 	public Map uploadPicture(MultipartFile uploadFile) {
 75 		Map  resultMap =new HashMap<>();
 76 		try {
 77 			/*
 78 			 * 生成一個新的文件名
 79 			 */
 80 			//取原文件名
 81 			  String  oldName=uploadFile.getOriginalFilename();
 82 			 //生成新文件名
 83 			  String newName = IDUtils.genImageName();
 84 			  newName=newName+oldName.substring(oldName.indexOf("."), oldName.length());
 85 			  // 圖片上傳 
 86 			  String imagePath=new DateTime().toString("/yyyy/MM/dd");
 87 			  System.out.println(FTP_PORT);
 88 			 boolean result= FtpUtil.uploadFile(FTP_ADDRESS,Integer.parseInt(FTP_PORT.trim()) , FTP_USERNAME, FTP_PASSWORD, FTP_BASEPATH, imagePath, newName, uploadFile.getInputStream());
 89 			if(!result){
 90 				resultMap.put("error", 1);
 91 				resultMap.put("message", "文件上傳失敗");
 92 				return resultMap;
 93 			}
 94 			resultMap.put("error", 0);
 95 			resultMap.put("url", IMAGE_BASE_URL+imagePath+"/"+newName);
 96 
 97 			return resultMap ;
 98 		} catch (IOException e) {
 99 			resultMap.put("error", 1);
100 			resultMap.put("message", "文件上傳發生異常"+e.toString());
101 			return resultMap;
102 		}
103 
104 
105 	}
106 
107 
108 
109 
110 
111 }

 

2-5: dao 層

無實現

 

2-6:工具類

  1 package com.taotao.common.utils;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.OutputStream;
 10 
 11 import org.apache.commons.net.ftp.FTP;
 12 import org.apache.commons.net.ftp.FTPClient;
 13 import org.apache.commons.net.ftp.FTPFile;
 14 import org.apache.commons.net.ftp.FTPReply;
 15 
 16 /**
 17  *
 18  * @ClassName:  FtpUtil
 19  * @Description: ftp上傳下載工具類
 20  *  來自傳智播客
 21  * @author:  劉軍/shall_liu(1136808529@qq.com)
 22  * @date:   2017年8月26日 下午7:10:41
 23  *
 24  * @Copyright: 2017
 25  */
 26 public class FtpUtil {
 27 
 28 	/**
 29 	 * Description: 向FTP服務器上傳文件
 30 	 * @param host FTP服務器hostname
 31 	 * @param port FTP服務器端口
 32 	 * @param username FTP登錄賬號
 33 	 * @param password FTP登錄密碼
 34 	 * @param basePath FTP服務器基礎目錄
 35 	 * @param filePath FTP服務器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑為basePath+filePath
 36 	 * @param filename 上傳到FTP服務器上的文件名
 37 	 * @param input 輸入流
 38 	 * @return 成功返回true,否則返回false
 39 	 */
 40 	public static boolean uploadFile(String host, int port, String username, String password, String basePath,
 41 			String filePath, String filename, InputStream input) {
 42 		boolean result = false;
 43 		FTPClient ftp = new FTPClient();
 44 		try {
 45 			int reply;
 46 			ftp.connect(host, port);// 連接FTP服務器
 47 			// 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
 48 			ftp.login(username, password);// 登錄
 49 			reply = ftp.getReplyCode();
 50 			if (!FTPReply.isPositiveCompletion(reply)) {
 51 				ftp.disconnect();
 52 				return result;
 53 			}
 54 			//切換到上傳目錄
 55 			if (!ftp.changeWorkingDirectory(basePath+filePath)) {
 56 				//如果目錄不存在創建目錄
 57 				String[] dirs = filePath.split("/");
 58 				String tempPath = basePath;
 59 				for (String dir : dirs) {
 60 					if (null == dir || "".equals(dir)) continue;
 61 					tempPath += "/" + dir;
 62 					if (!ftp.changeWorkingDirectory(tempPath)) {
 63 						if (!ftp.makeDirectory(tempPath)) {
 64 							return result;
 65 						} else {
 66 							ftp.changeWorkingDirectory(tempPath);
 67 						}
 68 					}
 69 				}
 70 			}
 71 			//設置上傳文件的類型為二進制類型
 72 			ftp.setFileType(FTP.BINARY_FILE_TYPE);
 73 			//上傳文件
 74 			if (!ftp.storeFile(filename, input)) {
 75 				return result;
 76 			}
 77 			input.close();
 78 			ftp.logout();
 79 			result = true;
 80 		} catch (IOException e) {
 81 			e.printStackTrace();
 82 		} finally {
 83 			if (ftp.isConnected()) {
 84 				try {
 85 					ftp.disconnect();
 86 				} catch (IOException ioe) {
 87 				}
 88 			}
 89 		}
 90 		return result;
 91 	}
 92 
 93 	/**
 94 	 * Description: 從FTP服務器下載文件
 95 	 * @param host FTP服務器hostname
 96 	 * @param port FTP服務器端口
 97 	 * @param username FTP登錄賬號
 98 	 * @param password FTP登錄密碼
 99 	 * @param remotePath FTP服務器上的相對路徑
100 	 * @param fileName 要下載的文件名
101 	 * @param localPath 下載后保存到本地的路徑
102 	 * @return
103 	 */
104 	public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
105 			String fileName, String localPath) {
106 		boolean result = false;
107 		FTPClient ftp = new FTPClient();
108 		try {
109 			int reply;
110 			ftp.connect(host, port);
111 			// 如果采用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
112 			ftp.login(username, password);// 登錄
113 			reply = ftp.getReplyCode();
114 			if (!FTPReply.isPositiveCompletion(reply)) {
115 				ftp.disconnect();
116 				return result;
117 			}
118 			ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄
119 			FTPFile[] fs = ftp.listFiles();
120 			for (FTPFile ff : fs) {
121 				if (ff.getName().equals(fileName)) {
122 					File localFile = new File(localPath + "/" + ff.getName());
123 
124 					OutputStream is = new FileOutputStream(localFile);
125 					ftp.retrieveFile(ff.getName(), is);
126 					is.close();
127 				}
128 			}
129 
130 			ftp.logout();
131 			result = true;
132 		} catch (IOException e) {
133 			e.printStackTrace();
134 		} finally {
135 			if (ftp.isConnected()) {
136 				try {
137 					ftp.disconnect();
138 				} catch (IOException ioe) {
139 				}
140 			}
141 		}
142 		return result;
143 	}
144 
145 	public static void main(String[] args) {
146 		try {
147 	        FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg"));
148 	        boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);
149 	        System.out.println(flag);
150 	    } catch (FileNotFoundException e) {
151 	        e.printStackTrace();
152 	    }
153 	}
154 }
155 

 

  1 package com.taotao.common.utils;
  2 
  3 import java.util.Random;
  4 
  5 /**
 6  *
 7  * @ClassName:  IDUtils
 8  * @Description:各種id生成策略
 9  * @author:  劉軍/shall_liu(1136808529@qq.com)
 10  * @date:   2017年8月26日 下午2:49:33
 11  *
 12  * @Copyright: 2017
 13  */
 14 public class IDUtils {
 15 
 16 	/**
 17 	 * 圖片名生成
 18 	 */
 19 	public static String genImageName() {
 20 		//取當前時間的長整形值包含毫秒
 21 		long millis = System.currentTimeMillis();//毫秒
 22 		//納秒   1秒=1000毫秒=1000 X 1000 微秒 =1000 X 1000X 1000 納秒 
 23 		//long millis = System.nanoTime(); 
 24 		//加上三位隨機數
 25 		Random random = new Random();
 26 		int end3 = random.nextInt(999);
 27 		//如果不足三位前面補0
 28 		String str = millis + String.format("%03d", end3);
 29 
 30 		return str;
 31 	}
 32 
 33 	/**
 34 	 * 商品id生成
 35 	 */
 36 	public static long genItemId() {
 37 		//取當前時間的長整形值包含毫秒
 38 		long millis = System.currentTimeMillis();
 39 		//long millis = System.nanoTime();
 40 		//加上兩位隨機數
 41 		Random random = new Random();
 42 		int end2 = random.nextInt(99);
 43 		//如果不足兩位前面補0
 44 		String str = millis + String.format("%02d", end2);
 45 		long id = new Long(str);
 46 		return id;
 47 	}
 48 
 49 	public static void main(String[] args) {
 50 		for(int i=0;i< 100;i++)
 51 		System.out.println(genItemId());
 52 	}
 53 }
 54 

 

  1 package com.taotao.common.utils;
  2 
  3 import java.util.List;
  4 
  5 import com.fasterxml.jackson.core.JsonProcessingException;
  6 import com.fasterxml.jackson.databind.JavaType;
  7 import com.fasterxml.jackson.databind.ObjectMapper;
  8 
  9 /**
 10  * 淘淘商城自定義響應結構
 11  */
 12 public class JsonUtils {
 13 
 14     // 定義jackson對象
 15     private static final ObjectMapper MAPPER = new ObjectMapper();
 16 
 17     /**
 18      * 將對象轉換成json字符串。
 19      * <p>Title: pojoToJson</p>
 20      * <p>Description: </p>
 21      * @param data
 22      * @return
 23      */
 24     public static String objectToJson(Object data) {
 25     	try {
 26 			String string = MAPPER.writeValueAsString(data);
 27 			return string;
 28 		} catch (JsonProcessingException e) {
 29 			e.printStackTrace();
 30 		}
 31     	return null;
 32     }
 33 
 34     /**
 35      * 將json結果集轉化為對象
 36      *
 37      * @param jsonData json數據
 38      * @param clazz 對象中的object類型
 39      * @return
 40      */
 41     public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
 42         try {
 43             T t = MAPPER.readValue(jsonData, beanType);
 44             return t;
 45         } catch (Exception e) {
 46         	e.printStackTrace();
 47         }
 48         return null;
 49     }
 50 
 51     /**
 52      * 將json數據轉換成pojo對象list
 53      * <p>Title: jsonToList</p>
 54      * <p>Description: </p>
 55      * @param jsonData
 56      * @param beanType
 57      * @return
 58      */
 59     public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
 60     	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
 61     	try {
 62     		List<T> list = MAPPER.readValue(jsonData, javaType);
 63     		return list;
 64 		} catch (Exception e) {
 65 			e.printStackTrace();
 66 		}
 67 
 68     	return null;
 69     }
 70 
 71 }
 72 

 

2-7:對於 linux環境下的nginx 服務器和ftp服務器的搭建 請查看以下文章:

centos7_ linux : Nginx安裝手冊》、《CentOS7 搭建FTP服務器》、《centos7 nginx圖片 服務器可以訪問ftp用戶上傳的圖片資源的配置

 

 

四:效果展示:

image

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM