import io.swagger.annotations.*; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.*; import org.apache.http.util.EntityUtils; import org.springframework.web.bind.annotation.*; import java.io.*; @RestController @RequestMapping(value = "/web-dav") @Api(value = "WebDav", tags = {"WebDav"}) @CrossOrigin //跨域访问 swagger 要使用到 public class WebDavController { @GetMapping(value = "upload") @ApiOperation(value = "上传文件") @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")}) public boolean upload(@ApiParam(value = "Token字符串", required = true) @RequestHeader(value = "Authorization") String token) { try { File file = new File("D:\\aa.pdf"); CredentialsProvider credentialsPovider = new BasicCredentialsProvider(); Credentials creds = new UsernamePasswordCredentials("roger", "123456"); credentialsPovider.setCredentials(AuthScope.ANY, creds); HttpClientBuilder clientbuilder = HttpClients.custom(); clientbuilder = clientbuilder.setDefaultCredentialsProvider(credentialsPovider); CloseableHttpClient httpclient = clientbuilder.build(); HttpPut put = new HttpPut("http://192.168.0.111:15108/aa.pdf"); ByteArrayEntity entity = new ByteArrayEntity(file2byte(file)); put.setEntity(entity); CloseableHttpResponse response = httpclient.execute(put); System.out.println(covertEntityToJSON(response.getEntity())); }catch (Exception e){ e.printStackTrace(); } return true; } private static String covertEntityToJSON(HttpEntity entity) throws Exception { if (entity != null) { String res = EntityUtils.toString(entity, "UTF-8"); return res; } else { return null; } } private byte[] file2byte(File file){ byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1){ bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); }catch (Exception e){ e.printStackTrace(); } return buffer; } }
方式二
import io.swagger.annotations.*; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.*; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; import org.springframework.web.bind.annotation.*; import java.io.*; @RestController @RequestMapping(value = "/web-dav2") @Api(value = "WebDav", tags = {"WebDav"}) @CrossOrigin //跨域访问 swagger 要使用到 public class WebDavController2 { @GetMapping(value = "upload") @ApiOperation(value = "上传文件") @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")}) public boolean upload(@ApiParam(value = "Token字符串", required = true) @RequestHeader(value = "Authorization") String token) { try { File file = new File("D:\\aa.pdf"); CredentialsProvider credentialsPovider = new BasicCredentialsProvider(); Credentials creds = new UsernamePasswordCredentials("roger", "123456"); credentialsPovider.setCredentials(AuthScope.ANY, creds); ClientConnectionManager connManager = new PoolingClientConnectionManager(); DefaultHttpClient client = new DefaultHttpClient(connManager); client.setCredentialsProvider(credentialsPovider); HttpPut put = new HttpPut("http://192.168.0.111:15108/aa.pdf"); ByteArrayEntity entity = new ByteArrayEntity(file2byte(file)); put.setEntity(entity); CloseableHttpResponse response = client.execute(put); System.out.println(covertEntityToJSON(response.getEntity())); }catch (Exception e){ e.printStackTrace(); } return true; } private static String covertEntityToJSON(HttpEntity entity) throws Exception { if (entity != null) { String res = EntityUtils.toString(entity, "UTF-8"); return res; } else { return null; } } private byte[] file2byte(File file){ byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1){ bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); }catch (Exception e){ e.printStackTrace(); } return buffer; } }
判断webdav信息是否正确
@GetMapping(value = "exist") @ApiOperation(value = "信息是否正确") @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")}) public boolean exist(@ApiParam(value = "Token字符串", required = true) @RequestHeader(value = "Authorization") String token) { try { CredentialsProvider credentialsPovider = new BasicCredentialsProvider(); Credentials creds = new UsernamePasswordCredentials("roger", "1234567"); credentialsPovider.setCredentials(AuthScope.ANY, creds); HttpClientBuilder clientbuilder = HttpClients.custom(); clientbuilder = clientbuilder.setDefaultCredentialsProvider(credentialsPovider); CloseableHttpClient httpclient = clientbuilder.build(); HttpHead head = new HttpHead("http://192.168.190.121:15108"); CloseableHttpResponse response = httpclient.execute(head); StatusLine statusLine = response.getStatusLine(); return isGoodResponse(statusLine.getStatusCode()); }catch (Exception e){ e.printStackTrace(); } return false; }