当前位置: 首页 > news >正文

网站定制电话内江 网站建设

网站定制电话,内江 网站建设,wordpress用户角色权限管理,稿定设计在线制作官网准备 确定好服务器端文件保存的位置确定好请求参数名&#xff08;前后端要保持一致的喔&#xff09;如果手机是通过usb连接到电脑的&#xff0c;需要执行一下&#xff1a; adb reverse tcp:8080 tcp:8080 AndroidManifest.xml的<application/>节点中加上: android:usesC…

准备

  • 确定好服务器端文件保存的位置
  • 确定好请求参数名(前后端要保持一致的喔)
  • 如果手机是通过usb连接到电脑的,需要执行一下:
    • adb reverse tcp:8080 tcp:8080
  • AndroidManifest.xml<application/>节点中加上:
    • android:usesCleartextTraffic="true"
  • 引入依赖:
    • implementation("com.google.net.cronet:cronet-okhttp:0.1.0")

开始

Android端

Activity(ComponentActivity)private lateinit var imagePicker: ActivityResultLauncher<PickVisualMediaRequest>override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)imagePicker = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) {context.contentResolver.openInputStream(it)?.use {val file = File(File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "TempFiles").smartCreate(true), "Image_${currentTimeMillis}.png").smartCreate()it.copyTo(FileOutputStream(file))launch(Dispatchers.Main) { launch(Dispatchers.IO) { val clint = OkHttpClient()val requestBody = MultipartBody.Builder().apply {setType(MultipartBody.FORM)// 这个 "file" 是前后端参数名,保持一致,不然springboot报错addFormDataPart("file", imageFile!!.name, RequestBody.create(MediaType.parse("image/*"),imageFile))}.build()val responseBody = clint.newCall(Request.Builder().post(requestBody).url("http://localhost:8080/upload").build()).execute().body()logE { "${responseBody?.string()}" }}}}}
}inline val currentTimeMillis: Longget() = System.currentTimeMillis()fun File.smartCreate(isDir: Boolean = false): File {if (!exists()) {parentFile?.mkdirs()if (isDir) mkdir() else createNewFile()}return this
}

服务端(Spring Boot)

1.在application.properties文件中配置文件相关的参数

spring.servlet.multipart.max-request-size=50MB
spring.servlet.multipart.max-file-size=50MB# 上传的文件保存在哪个文件下,这里保存到项目文件夹下的upload文件夹
# 也可以指定其他文件夹,把路径复制上就行,比如 upload.file.path=C:\Users\Public\Pictures
upload.file.path=upload

2.写Controller方法

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
public class TestController {@Value("${upload.file.path}")private String uploadPathStr;/*** 保持请求参数名一致* * @param file* @return*/@PostMapping("/upload")public @ResponseBody boolean upload(@RequestParam("file") MultipartFile file){if(file == null || file.isEmpty() || filename == null || filename.isEmpty())return false;try(InputStream inputStream = file.getInputStream()) {Path uploadPath = Paths.get(uploadPathStr);if(!uploadPath.toFile().exists())uploadPath.toFile().mkdirs();Files.copy(inputStream, Paths.get(uploadPathStr).resolve(file.getOriginalFilename()), StandardCopyOption.REPLACE_EXISTING);System.out.println("upload file , filename is "+file.getOriginalFilename() + ", filePath = " + Paths.get(uploadPathStr).resolve(file.getOriginalFilename()).toAbsolutePath().toString());return true;}catch (IOException e) {e.printStackTrace();return false;}}
}

运行调试即可…

http://www.yayakq.cn/news/210282/

相关文章:

  • 网站外包开发 代码的版权问题网站模板套用湖南岚鸿
  • 泰州住房城乡建设网站个体户做网站与公司好
  • 合肥最好的网站建设公司排名wordpress怎样美化网页
  • 手表网站素材电商网站开发选题依据
  • 湖北省建设信息港网站wordpress上传图片时发生了错误
  • 做区域县城招聘网站永济网站建设
  • 有织梦后台系统怎么做网站软件开发收费价目表
  • 浙江做公司网站多少钱用户体验设计专业
  • 网站建设费用计入哪个会计科目wordpress码字主题
  • 自己做网站 教程设计招聘网站
  • 使用vue做单页面网站wordpress磁力链插件
  • 手机网站头部网站开发是前端还是后端
  • 哪些网站可以做商家网站栏目词
  • 长沙住房与城乡建设部网站wordpress 如何删除所有评论
  • google属于搜索引擎类网站.制作html代码
  • 网站建设的基础知识与维护wordpress古文主题
  • 深圳英文网站制作什么叫整合营销
  • 普陀区建设工程质检网站义乌门户网站建设
  • 有没有教做帽子的网站兰州网站开发公司
  • 网站设计技术手机wordpress查看加密文章
  • 万博法务网站建设项目删除标题wordpress
  • 在哪个网站上可以找兼职做新加坡域名注册网站
  • 网站管理员密码杨凌网站建设公司
  • 遵义水网站建设网站开发会计科目
  • 网站建设这个行业怎么样网站建设如何定价
  • 一站式营销推广点击器免费版
  • wordpress转为app国外网站seo
  • 布局网站建设护肤品网站建设前的行业分析
  • 做网站需要多久优客逸家网站建设
  • 太原网站推广服务网站开发一个多少钱啊