10天冲刺第四天
今天做了什么:
springboot服务器搭建,实现图片上传
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"; } }
咨询师资格申请
页面:
<?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>
代码:
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知识问题