我要把本地的文件上传到服务器上面去: <p:fileUpload value="#{resourceListAction.file}" id="file" mode="simple"></p:fileUpload> <p:commandButton value="上传文件" action="#{resourceListAction.insertResource()}" type="submit" ajax="false" update="reFreshData"> </p:commandButton> 后端insertResource方法调用upload方法 /** * 上传文件到服务器 */ public boolean upload(UploadedFile file,String resname) throws Exception{ Saml2SignResponse response = (Saml2SignResponse) JsfHelper.getSessionValue("loginUser"); String tenantId = response.getTenantId(); String savePath = "http://www.pdsdemo.com:8280/fsc/v1/"+tenantId+"/"+resname; try{ InputStream in = new BufferedInputStream(file.getInputstream()); try{ byte[] buffer = new byte[64*1024]; FileOutputStream fileOutputStream = new FileOutputStream(savePath); while(in.read(buffer)>0){ fileOutputStream.write(buffer); } }finally{ in.close(); } return true; }catch (Exception e) { // TODO: handle exception FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL,e.getClass().getName(),e.getMessage()); FacesContext.getCurrentInstance().addMessage(null, message); return false; } } savePath为路径 ,但是服务器端有鉴权(身份认证),鉴权通过才能上传文件,需要在请求头 加参数Authorization 有一个demo是用jsp 、ajaxForm来搞的 设置了请求头: function selectfile(){ var ofileName = $("#upload_file_input"); var fname = ofileName[0].value; fname = fname.replace(/\/g, "/"); var filename=fname.substr(fname.lastIndexOf(""/"")+1, fname.length); var options = { target: ""#upload_file_input"", url:""http://www.pdsdemo.com:8280/fsc/v1/""+filename, type:"POST", beforeSend:function(request){ request.setRequestHeader("Authorization", "Bearer <%=token%>"); }, success: function(data) { var savefilename = data.path.substr(data.path.lastIndexOf(""/"")+1, data.path.length); window.opener.document.getElementById("concreteMaterial.filename").value = savefilename; window.opener.document.getElementById("concreteMaterial.filesize").value = data.size; } }; // 将options传给ajaxForm $(""#upload_form"").ajaxForm(options); } <form method="POST" id="upload_form" > <input id="upload_file_input" type="file" size="3" name="file" onchange="selectfile();"/> <input type="submit" name="submit" value="submit"> </form> 我该如何改造我的代码达到效果呢? 各位大牛求方案 |
|
可以使用filter 解决吧… 或者删除权限了..上传文件
|
|
40分 |
你这是 服务端和客户端要验证TOKEN,你给的例子上,人家用的是http POST请求,在http head里添加 Authorization,用来存token。
你要学着来,就按下面代码 public class Upload { public Upload(){ String filePath="D:\DataSocketServer.java"; String fileName="DataSocketServer.java"; try{ URL url=new URL("http://localhost:8080//TestServlet3"); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream()); File file=new File(filePath); FileInputStream fis=new FileInputStream(file); byte[] bytes=new byte[1024]; int numReadByte=0; while((numReadByte=fis.read(bytes,0,1024))>0){ out.write(bytes,0,numReadByte); } out.flush(); fis.close(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ new Upload(); } } |