1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 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; } } |
方式二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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信息是否正确
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @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 ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了