开发区图片上传到我们的服务器

This commit is contained in:
majintao
2024-10-10 14:42:44 +08:00
parent f46b08db06
commit df24d19b94
6 changed files with 81 additions and 39 deletions
@@ -3,9 +3,11 @@ package com.fangjia.bkoff.controllers;
import com.fangjia.bkoff.model.DevelopmentZoneModel; import com.fangjia.bkoff.model.DevelopmentZoneModel;
import com.fangjia.bkoff.model.JsonModel; import com.fangjia.bkoff.model.JsonModel;
import com.fangjia.bkoff.model.LatlngPolygonModel; import com.fangjia.bkoff.model.LatlngPolygonModel;
import com.fangjia.bkoff.util.ExcelUtils;
import com.fangjia.core.util.GeoUtils; import com.fangjia.core.util.GeoUtils;
import com.fangjia.core.util.ThreadLocalData; import com.fangjia.core.util.ThreadLocalData;
import com.fangjia.core.util.ThreadLocalKey; import com.fangjia.core.util.ThreadLocalKey;
import com.fangjia.dal.QueryParam;
import com.fangjia.dal.mongo.model.DevelopmentZone; import com.fangjia.dal.mongo.model.DevelopmentZone;
import com.fangjia.dal.mongo.model.LatlngPolygon; import com.fangjia.dal.mongo.model.LatlngPolygon;
import com.fangjia.dal.mongo.model.StatsIndex; import com.fangjia.dal.mongo.model.StatsIndex;
@@ -13,9 +15,19 @@ import com.fangjia.dal.mongo.model.bkoff.BkoffUsers;
import com.fangjia.service.mongo.bkoff.BkoffUsersService; import com.fangjia.service.mongo.bkoff.BkoffUsersService;
import com.fangjia.service.mongo.fangjia.DevelopmentZoneService; import com.fangjia.service.mongo.fangjia.DevelopmentZoneService;
import com.fangjia.service.mongo.fangjia.LatlngPolygonService; import com.fangjia.service.mongo.fangjia.LatlngPolygonService;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.sothis.core.util.Pager; import org.sothis.core.util.Pager;
import org.sothis.web.mvc.ActionContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*; import java.util.*;
import static com.fangjia.core.util.GeoUtils.LAT; import static com.fangjia.core.util.GeoUtils.LAT;
@@ -214,7 +226,7 @@ public class DevelopmentZoneController {
if (StringUtils.isNotBlank(model.getDistrict())) { if (StringUtils.isNotBlank(model.getDistrict())) {
udata.put("district", model.getDistrict()); udata.put("district", model.getDistrict());
} }
if (StringUtils.isNotBlank(model.getImgUrl())){ if (StringUtils.isNotBlank(model.getImgUrl())) {
udata.put("imgUrl", model.getImgUrl()); udata.put("imgUrl", model.getImgUrl());
} }
udata.put("createTime", new Date()); udata.put("createTime", new Date());
@@ -272,5 +284,22 @@ public class DevelopmentZoneController {
return gdLocationList; return gdLocationList;
} }
public Object uploadAction(DevelopmentZoneModel model) throws IOException, UnirestException, JSONException {
JsonModel ret = new JsonModel();
Map<String, Object> data = new HashMap<String, Object>();
File file = model.getFile();
HttpResponse<String> response = Unirest.post("http://open.fangjia.com/files/uploadGridFs")
.field("file", file)
.field("token", "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB")
.asString();
// 获取返回的链接
JSONObject jsonResponse = new JSONObject(response.getBody());
String link = jsonResponse.getJSONObject("result").getString("link");
data.put("imgUrl", link);
data.put("msg", "图片上传成功");
ret.setJsonModel(data);
return ret;
}
} }
@@ -3,6 +3,7 @@ package com.fangjia.bkoff.model;
import com.fangjia.dal.mongo.model.DevelopmentZone; import com.fangjia.dal.mongo.model.DevelopmentZone;
import com.fangjia.dal.mongo.model.LatlngPolygon; import com.fangjia.dal.mongo.model.LatlngPolygon;
import java.io.File;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -37,6 +38,8 @@ public class DevelopmentZoneModel extends BaseModel {
private String imgUrl; private String imgUrl;
private File file;
public String getAlias() { public String getAlias() {
return alias; return alias;
} }
@@ -204,4 +207,12 @@ public class DevelopmentZoneModel extends BaseModel {
public void setImgUrl(String imgUrl) { public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl; this.imgUrl = imgUrl;
} }
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
} }
@@ -188,38 +188,32 @@
document.getElementById('fileInput').addEventListener('change', async () => { document.getElementById('fileInput').addEventListener('change', async () => {
const fileInput = document.getElementById('fileInput'); const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0]; const file = fileInput.files[0];
const token = "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB";
if (!file) { if (!file) {
alert('请选择一个文件!'); alert('请选择一个文件!');
return; return;
} }
// 使用 FormData 传输文件
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
formData.append('token', token); // 将 token 作为请求参数
try { $.ajax({
// 发送 POST 请求上传文件 url: '../developmentZone/upload',
const response = await fetch('http://open.fangjia.com/files/uploadGridFs', { type: 'POST',
method: 'POST', data: formData,
body: formData, cache: false,
}); contentType: false,
processData: false,
const result = await response.json(); success: function (data) {
document.getElementById('imgUrl').value = JSON.parse(data).imgUrl;
if (result.result.link) { // 假设返回值包含图片URL alert('上传成功!');
console.log(result.result.link); console.log(data);
document.getElementById('imgUrl').value = result.result.link; // 将图片URL赋值给 imgUrl 输入框 },
} else { error: function (returndata) {
alert('上传失败,未能获取图片URL'); alert("上传失败!");
} }
});
document.getElementById('response').innerText = '上传成功';
} catch (error) {
console.error('上传失败:', error);
document.getElementById('response').innerText = '上传失败,请查看控制台。';
}
}); });
</script> </script>
<script> <script>
@@ -29,7 +29,8 @@
<input id="id" name="id" type="hidden" value="${developmentZone.id!}"/> <input id="id" name="id" type="hidden" value="${developmentZone.id!}"/>
</#if> </#if>
<div style="float: left"> <div style="float: left">
<img id="myImage" src="${developmentZone.imgUrl!'/path/to/default/image.jpg'}"/> <img id="myImage" src="${developmentZone.imgUrl!'/path/to/default/image.jpg'}"
style="width: 800px; height: 800px; object-fit: contain;"/>
<#-- <img id="myImage" src="${developmentZone.imgUrl}"/>--> <#-- <img id="myImage" src="${developmentZone.imgUrl}"/>-->
<br/> <br/>
<input id="draw" type="button" value="画图"/> <input id="draw" type="button" value="画图"/>
@@ -45,12 +46,12 @@
</div> </div>
<div> <div>
<div> <div>
<@d_city defaultValue="<#if developmentZone??>${developmentZone.cityName1!}</#if>" callback="changeCity" callbackField="display" name="cityDisplay" /> <@d_city defaultValue="上海" callback="changeCity" callbackField="display" name="cityDisplay" showCountry = "1" />
</div> </div>
<input type="text" id="seaAddress"/> <input type="text" id="seaAddress"/>
<input type="button" id="btn_sea" value="查询"/> <input type="button" id="btn_sea" value="查询"/>
<input id="saveOrUpdate" type="button" value="提交"/> <input id="saveOrUpdate" type="button" value="提交"/>
<input name="bdShape" id="bdShape" type="hidden"/> <input name="bdShape" id="bdShape" type="hidden"/>
<div id="allmap" style="height:900px;margin-left: -30px"></div> <div id="allmap" style="height:900px;margin-left: -30px"></div>
</div> </div>
@@ -68,15 +68,19 @@
<td>区块</td> <td>区块</td>
<td>${developmentZone.block!}</td> <td>${developmentZone.block!}</td>
</tr> </tr>
</table>
</form>
<table style="margin-top: -100px;border: none">
<tr> <tr>
<td>开发区图片</td> <#-- <td>开发区图片</td>-->
<td><img id="myImage" src="${developmentZone.imgUrl!'/path/to/default/image.jpg'}"/></td> <td><img id="myImage" src="${developmentZone.imgUrl!'/path/to/default/image.jpg'}"
style="width: 800px; height: 800px; object-fit: contain;"/></td>
<td colspan="2"> <td colspan="2">
<div id="allmap" style="width:1024px;height:600px;"></div> <div id="allmap" style="width:1024px;height:500px;"></div>
</td> </td>
</tr> </tr>
</table> </table>
</form>
</div> </div>
</div> </div>
@@ -111,10 +115,13 @@
<#--];--> <#--];-->
// 处理coords的格式 // 处理coords的格式
var coords = [ var coords = [
<#list developmentZone.bdShape.coordinates as latlng> <#list developmentZone.bdShape.coordinates as latlng>
{ lat: ${latlng[1]?string("#.######")}, lng: ${latlng[0]?string("#.######")} }<#if latlng_index lt developmentZone.bdShape.coordinates?size>, </#if> {
</#list> lat: ${latlng[1]?string("#.######")},
lng: ${latlng[0]?string("#.######")}
}<#if latlng_index lt developmentZone.bdShape.coordinates?size>, </#if>
</#list>
]; ];
// // 计算面积 // // 计算面积
// function calculateArea(coords) { // function calculateArea(coords) {
@@ -159,6 +166,7 @@
const conversionFactor = 111.32; // 大约每度经纬度的公里数(考虑到地球的曲率) const conversionFactor = 111.32; // 大约每度经纬度的公里数(考虑到地球的曲率)
return area * conversionFactor * conversionFactor; // 返回面积(平方公里) return area * conversionFactor * conversionFactor; // 返回面积(平方公里)
} }
const areaInDegrees = calculateArea(coords); const areaInDegrees = calculateArea(coords);
// const areaInSquareKilometers = areaInDegrees * 123.5; // 转换为平方公里 // const areaInSquareKilometers = areaInDegrees * 123.5; // 转换为平方公里
const areaInSquareKilometers = areaInDegrees; // 转换为平方公里 const areaInSquareKilometers = areaInDegrees; // 转换为平方公里
@@ -137,7 +137,6 @@
} }
} }
var file = new FormData($("#file")[0]); var file = new FormData($("#file")[0]);
debugger
$.ajax({ $.ajax({
url: '../villageIdentify/upload', url: '../villageIdentify/upload',
type: 'POST', type: 'POST',