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

网站编辑岗位门户网站 解决方案

网站编辑岗位,门户网站 解决方案,邢台市官网,哪些网站是用响应式布局做的准备 确定好服务器端文件保存的位置确定好请求参数名&#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/506553/

相关文章:

  • 厦门正规网站建设公司网络免费推广网站
  • 魔立方 网站建设北京哪家做网站
  • 网站栏目功能网页设计素材保存
  • php网站虚拟机政务新网站建设
  • 网站管理制度建设的情况网页制作模板文件的扩展名
  • 外贸类网站建设网站建设栏目
  • 咸阳网站建设有哪些wordpress 文章 链接 手动添加 nofollow
  • 温江网站建设价格肥城网站开发公司
  • 网站 留言 以邮件形式网站服务器建设商
  • 网站代码上传到服务器后要怎么做的代做网站多少钱
  • 在小型网站建设小组中的基本可以用什么做网站登录页面
  • 网站seo化自己可以吗如何用ps来做网站设计
  • 贵州城乡建设厅施工员报名网站网站建设实训意义
  • 怎么做情侣网站阿里云WordPress一键安装
  • 网站地图插件网站建设公司营业执照图片
  • 安徽网站设计费用哈尔滨最新通知
  • 网站开发公司 苏州如何创建网站页面
  • 装潢公司网站设计与制作dedecms网站上传服务器不是空间
  • php网站开发入门土特产直营网站建设代码
  • 企业网站建设论文模板柳州网站
  • 有没有做网站的教程网页设计一单多少钱
  • 珠海网站空间注册wordpress文章时间轴
  • 大学生婚恋网站策划书只做二手奢侈品的网站
  • 百度网站排名怎么提高金融公司网站模版
  • discuz!网站模板室内设计学校大专
  • 网站规划与设计h5高端建站的公司
  • 如何建微信微商城网站新乡手机网站建设服务
  • 苏州市住房和城乡建设局官方网站网站运营推广公司
  • 购物网站开发问题域分析锦州网站开发招聘
  • 网站制作公司 首推万维科技济南网站建设选搜点网络VIP