dropzone-Httpclient
作者:互联网
DropZone.js 和 JAVA远程调用接口
<body>
<form action=${ctx}/xyPtapllyOwn/saveUploadFile method="post" class="dropzone" name="filess" id="filess" enctype="multipart/form-data">
<div class="fallback">
<input name="filess" type="file" multiple />
</div>
</form>
</body>
<script type="text/javascript">
Dropzone.autoDiscover = false;
$(".dropzone").dropzone({
url : root + '/xyPtapllyOwn/saveUploadFile',
paramName: "filess",
maxFilesize: 2048, // 上传的文件大小
maxFiles:1,//一次性上传的文件数量上限
addRemoveLinks : true,//添加移除文件
autoProcessQueue: true,//不自动上传
dictCancelUploadConfirmation:'你确定要取消上传吗?',
dictMaxFilesExceeded: "只能上传{{maxFiles}}个文件",
dictFileTooBig:"文件过大({{filesize}}MB). 上传文件最大支持: {{maxFilesize}}MB.",
dictDefaultMessage :
'<span class="bigger-10 bolder"> </span> \
<span class="smaller-50 grey"></span> <br /> \
<i class="upload-icon icon-cloud-upload grey icon-3x" style="font-size: 26px">将文件拖拽到此处或点击打开文件管理器选择文件 再点击提交</i>',
dictResponseError: '文件上传失败!',
dictCancelUpload: "取消上传",
dictCancelUploadConfirmation: "你确定要取消上传吗?",
dictRemoveFile: "移除文件",
uploadMultiple:false,
autoProcessQueue: false,
init: function() {
var submitButton = document.querySelector("#submit-all")
myDropzone = this;
submitButton.addEventListener("click", function() {
myDropzone.processQueue();
});
//上传文件时触发的事件
this.on("addedfile", function(file) {
//浏览器控制台输出js
});
//上传文件成功时触发的事件
this.on("success", function(file, response) {
if (response == false) {
alert("上传失败, 请移除文件重新上传");
}
});
//移除文件触发的事件
this.on("removedfile", function(file) {
});
}
});
</script>
@RequestMapping(value = {"/saveUploadFile"}, method = RequestMethod.POST)
@ResponseBody
public String saveUploadFile(@RequestParam("filess") MultipartFile filess) throws Exception {
if (filess.isEmpty()) {
return "false";
}
// 根据名称获取自有文件夹路径
// filess.transferTo(new File(savePath + fileName)); // 保存文件
return "true";
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
远程调用
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.PrivateKey;
import java.util.Map;
public class ConnectedUtil {
//获取连接的方法
public static JSONObject getConnection(String URL, Map<String, Object> map) throws Exception {
String result = "";
// 建立连接
java.net.URL url = new URL(URL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置参数
httpConn.setDoOutput(true); // 需要输出
httpConn.setDoInput(true); // 需要输入
httpConn.setUseCaches(false); // 不允许缓存
httpConn.setRequestMethod("POST");// 设置POST方式连接
// 设置请求属性
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
// 连接,也可以不用明文connect,使用下面的httpConn.getOutputStream()会自动connect
httpConn.connect();
Object jsonObject = JSONObject.toJSON(map);
// JSONObject jsonObject = JSONObject.fromObject(map);
// 建立输入流,向指向的URL传入参数
DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
dos.write(jsonObject.toString().getBytes("UTF-8"));
dos.flush();
dos.close();
// 获得响应状态
JSONObject jsonmsg = null;
BufferedReader in = null;
int resultCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
// 解析json对象
// jsonmsg = JSONObject.fromObject(result);
// Object jsonObject = JSONObject.toJSON(map);
String PASSWORD = result;
PrivateKey privateKey = RSAUtil.string2PrivateKey(PRIVATEKEY);
byte[] base642Byte = RSAUtil.base642Byte(PASSWORD);
byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
PASSWORD = new String(privateDecrypt);
System.out.println("privateDecrypt = " + PASSWORD);
}
return jsonmsg;
}
}
标签:文件,java,JSONObject,httpConn,import,上传,dropzone,Httpclient 来源: https://blog.csdn.net/yuanpeij/article/details/121355028