java通过webdav实现文件上传
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;
   }

  

 

posted on   james-roger  阅读(2079)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示