10天冲刺第四天

今天做了什么:

springboot服务器搭建,实现图片上传

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.example.psychological.Controller;
 
import com.example.psychological.DBHelpOpen;
import com.example.psychological.Dao;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
@RestController
@RequestMapping("/apply")
@CrossOrigin
public class approveController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    /**
     *
     * @param uploadfile1 接收文件
     * @param name1 接收其余参数
     *@param uploadfile2 接收文件
     *@param name2 接收其余参数
     *@param name 接收其余参数
     *@param id 接收其余参数
     *@param money 接收其余参数
     *@param intro 接收其余参数
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
 
    public String upload(MultipartFile uploadfile1,MultipartFile uploadfile2, String name1,String name2,String name,String id,String money,String intro) throws IOException, SQLException {
 
        System.out.println(name);
        //2、将上传的图片存储到硬盘
        String path1="D:/code/Psychological/src/main/resources/"+name1;
        String path2="D:/code/Psychological/src/main/resources/"+name2;
        File dest1=new File(path1);
        uploadfile1.transferTo(dest1);
        File dest2=new File(path2);
        uploadfile2.transferTo(dest2);
        System.out.println(dest1.getPath());
        String sql1="insert into consultant(id,name,money,introduction,personUrl,applyUrl,state) values('"+id+"','"+name+"','"+money+"','"+intro+"','"+path2+"','"+path1+"','未审批')";
        String sql2="update consultant set name = '"+name+"',money = '"+money+"',introduction = '"+intro+"',state = '未审批' where id = '"+id+"'";
        Dao d =new Dao();
        Boolean y = d.haveCon(id);
        if (y){
            jdbcTemplate.update(sql2);
        }
        else {
            jdbcTemplate.update(sql1);
        }
        return "success";
    }
    @GetMapping("/download")
    public  void download(HttpServletResponse response, HttpServletRequest request) throws IOException, SQLException {
        String id = request.getHeader("id");
        String sql ="select * from consultant where id = '"+id+"'";
        Connection conn = DBHelpOpen.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        System.out.println(id);
        rs.next();
        String file = rs.getString("personUrl");
        InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer))!=-1) {
            outputStream.write(buffer,0,bytesRead);
        }
        inputStream.close();
        outputStream.close();
    }
    @GetMapping("/download2")
    public  void download2(HttpServletResponse response, HttpServletRequest request) throws IOException, SQLException {
        String id = request.getHeader("id");
        String sql ="select * from consultant where id = '"+id+"'";
        Connection conn = DBHelpOpen.getConn();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        System.out.println(id);
        rs.next();
        String file = rs.getString("applyUrl");
        InputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer))!=-1) {
            outputStream.write(buffer,0,bytesRead);
        }
        inputStream.close();
        outputStream.close();
    }
 
    /**
     *
     * @param uploadfile1 接收文件
     * @param name1 接收其余参数
     *@param name 接收其余参数
     *@param id 接收其余参数
     *@param money 接收其余参数
     *@param intro 接收其余参数
     * @return
     * @throws IOException
     */
 
    @PostMapping("/update")
 
    public String update(MultipartFile uploadfile1,String name1,String name,String id,String money,String intro) throws IOException {
 
        //2、将上传的图片存储到硬盘
        String path1="D:/code/Psychological/src/main/resources/"+name1;
        File dest1=new File(path1);
        uploadfile1.transferTo(dest1);
        String sql ="update consultant set name = '"+name+"',money = '"+money+"',introduction = '"+intro+"' where id = '"+id+"'";
        jdbcTemplate.update(sql);
        return "success";
    }
}

  

咨询师资格申请

页面:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".applyActivity">
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/white"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="证件照片:"
            android:textSize="20dp"
            android:textColor="@color/black"/>
        <ImageButton
            android:id="@+id/ibt_jpg"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/baseline_add_24"
            android:background="#BFBFBF" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="营业照片:"
            android:textSize="20dp"
            android:textColor="@color/black"/>
        <ImageButton
            android:id="@+id/ibt_person"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/baseline_add_24"
            android:background="#BFBFBF"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="预约金额:"
            android:textSize="20dp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/ed_money"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="真实姓名:"
            android:textSize="20dp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/ed_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="简介:"
            android:textSize="20dp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/ed_introduction"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:maxLength="50"/>
    </LinearLayout>
    <Button
        android:id="@+id/bt_commit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>
 
</LinearLayout>

  

代码:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.example.psychological;
 
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
 
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
 
import com.example.psychological.javaClass.Consultant;
import com.example.psychological.javaClass.HttpUtil;
import com.example.psychological.javaClass.Utils;
 
import java.io.File;
import java.io.IOException;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.Response;
 
public class applyActivity extends AppCompatActivity implements View.OnClickListener {
 
    private static final int STORAGE_PERMISSION = 1;
    private static final int CHOOSE_PHOTO = 1;
    private static final String NET="192.168.129.224";
    private ImageButton ibt_jpg;
    private ImageButton ibt_person;
    private Button bt_commit;
    private EditText ed_name;
    private EditText ed_money;
    private EditText ed_intro;
    private File file1=null;
    private File file2=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_apply);
        getSupportActionBar().hide();
        ibt_jpg = findViewById(R.id.ibt_jpg);
        ibt_person = findViewById(R.id.ibt_person);
        bt_commit = findViewById(R.id.bt_commit);
        ed_name = findViewById(R.id.ed_name);
        ed_money = findViewById(R.id.ed_money);
        ed_intro = findViewById(R.id.ed_introduction);
        ibt_jpg.setOnClickListener(this);
        ibt_person.setOnClickListener(this);
        bt_commit = findViewById(R.id.bt_commit);
        bt_commit.setOnClickListener(this);
    }
 
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.ibt_jpg){
            if (ContextCompat.checkSelfPermission(applyActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(applyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
            } else {
                xzImage1();
            }
        } else if (v.getId()==R.id.ibt_person) {
            if (ContextCompat.checkSelfPermission(applyActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(applyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
            } else {
                xzImage2();
            }
        }
        else {
            scImage();
            finish();
            startActivity(new Intent(this, MainActivity.class));
        }
    }
    private void xzImage1() {
 
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent,CHOOSE_PHOTO); // 打开本地存储
        //CHOOSE_PHOTO:全局常量,标识
    }
    private void xzImage2() {
 
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent,2); // 打开本地存储
        //CHOOSE_PHOTO:全局常量,标识
    }
    private void scImage() {
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        String id = sharedPreferences.getString("id","");
        String name = sharedPreferences.getString("name","");
        String applyname="apply/"+id+".jpg";
        String personname="person/"+id+".jpg";
        String realname=ed_name.getText().toString();
        String money=ed_money.getText().toString();
        String intro=ed_intro.getText().toString();
        if(file1==null||file2==null||realname.equals("")||personname.equals("")||money.equals("")||intro.equals("")) {
            Looper.prepare();
            Toast.makeText(applyActivity.this,"输入不符合要求", Toast.LENGTH_SHORT).show();
            Looper.loop();
        }
 
 
 
        //1、创建请求体
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)//请求类型
                .addFormDataPart("name1", applyname)//参数
                .addFormDataPart("name2", personname)
                .addFormDataPart("name",realname)
                .addFormDataPart("money",money)
                .addFormDataPart("intro",intro)
                .addFormDataPart("id",id)
                .addFormDataPart("uploadfile1", "uploadfile1", RequestBody.create(MediaType.parse("*/*"), file1))
                .addFormDataPart("uploadfile2", "uploadfile2", RequestBody.create(MediaType.parse("*/*"), file2))// 第一个参数传到服务器的字段名,第二个你自己的文件名,第三个MediaType.parse("*/*")数据类型,这个是所有类型的意思,file就是我们之前创建的全局file,里面是创建的图片
                .build();
        //2、调用工具类上传图片以及参数
        HttpUtil.uploadFile("http://"+NET+":8080/apply/upload", requestBody, new Callback() {
 
            //请求失败回调函数
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("=============");
                System.out.println("异常::");
                e.printStackTrace();
            }
 
            //请求成功响应函数
            @Override
            public void onResponse(Call call, Response response) throws IOException {
 
                showResponse(response.body().string());//在主线程中显示提示框
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String realPath;
 
        //requestCode:标识码
        //data:选择的图片的信息
        switch (requestCode) {
            case CHOOSE_PHOTO:
//显示图片
                ibt_jpg.setImageURI(data.getData());
                System.out.println("图片在手机上的虚拟路径为:"+data.getData());
                realPath = Utils.getRealPath(this, data);
                file1 = new File(realPath);
                System.out.println("图片在手机上的真实路径为:"+realPath);
                break;
            case 2:
                ibt_person.setImageURI(data.getData());
                System.out.println("图片在手机上的虚拟路径为:"+data.getData());
                realPath = Utils.getRealPath(this, data);
                file2 = new File(realPath);
                System.out.println("图片在手机上的真实路径为:"+realPath);
                break;
            default:
                break;
        }
    }
    //选择权限后的回调函数
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case STORAGE_PERMISSION:
                //检查是否有读取存储卡的权限,如果有则选择图片,如果没有则提示
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    xzImage1();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    xzImage2();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
    //ui操作,提示框
    private void showResponse(final String response) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 在这里进行UI操作,将结果显示到界面上
                Toast.makeText(applyActivity.this, response, Toast.LENGTH_SHORT).show();
            }
        });
    } 

明天:咨询申请后界面

遇到的问题:相册图片权限获取问题,springboot知识问题

 

posted @   umiQa  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示