COS文件上传

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">

<title>文件上传</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="foo/receiver.jsp" enctype="multipart/form-data" method="post">
<table align="center">
<tr><td>姓名</td><td><input type="text" name="name" value = "花花"/></td></tr>
<tr><td>性别</td><td><input type="text" name="sex" value="男"/></td></tr>
<tr><td>年龄</td><td><input type="text" name="age" value="108"/></td></tr>
<tr><td>文件1</td><td><input type="file" name="file" value=""/></td></tr>
<tr><td>文件2</td><td><input type="file" name="file" value=""/></td></tr>
<tr><td>文件3</td><td><input type="file" name="file" value=""/></td></tr>
<tr><td>文件4</td><td><input type="file" name="file" value=""/></td></tr>
<tr><td colspan="2"><input type="submit" value ="提交"/></td></tr>
</table>
</form>
</body>
</html>





receiver.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import ="com.oreilly.servlet.multipart.MultipartParser,com.oreilly.servlet.multipart.Part" %>
<jsp:directive.page import="com.oreilly.servlet.multipart.ParamPart"/>
<jsp:directive.page import="com.oreilly.servlet.multipart.FilePart"/>
<jsp:directive.page import="java.io.InputStream"/>
<jsp:directive.page import="java.io.BufferedOutputStream"/>
<jsp:directive.page import="java.io.OutputStream"/>
<jsp:directive.page import="java.io.FileOutputStream"/>
<jsp:directive.page import="java.io.File"/>
<%
MultipartParser mp = new MultipartParser(request,1024*1024*10);
mp.setEncoding("utf-8");
Part part;

while((part=mp.readNextPart())!=null){
String name = part.getName();

if(part.isParam()){
ParamPart pp = (ParamPart)part;
String value = pp.getStringValue();
out.println(name+"="+value);
}
else if(part.isFile()){
FilePart fp = (FilePart)part;
String filename = fp.getFileName();
String fileType = fp.getContentType();
String filePath = fp.getFilePath();
InputStream is = fp.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("e:/"+filename));
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] bt = new byte[2048];
int k ;
while(( k =is.read(bt))>0){
bos.write(bt,0,k);
}
out.println("文件名称:"+filename);
out.println("<br/>文件类型"+fileType);
out.println("<br/>文件路径:"+filePath);

}else{
out.println("File name:"+name);
}
}

%>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,你可以使用COS(腾讯云对象存储)来实现文件上传。以下是一个简单的示例: 1. 首先,需要在pom.xml文件中添加COS SDK的依赖: ```xml <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.6.9</version> </dependency> ``` 2. 在application.properties(或application.yml)文件中配置COS的相关信息: ```properties # COS配置 cos.secretId=your_secret_id cos.secretKey=your_secret_key cos.bucketName=your_bucket_name cos.region=your_bucket_region ``` 3. 创建一个文件上传的Controller: ```java import com.qcloud.cos.COSClient; import com.qcloud.cos.model.PutObjectRequest; import com.qcloud.cos.model.PutObjectResult; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @RestController public class FileUploadController { @Value("${cos.secretId}") private String secretId; @Value("${cos.secretKey}") private String secretKey; @Value("${cos.bucketName}") private String bucketName; @Value("${cos.region}") private String region; @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择要上传的文件"; } try { // 创建COSClient实例 COSClient cosClient = new COSClient(secretId, secretKey); // 设置存储桶所在的地域 cosClient.setRegion(region); // 生成文件在COS中的唯一键 String key = file.getOriginalFilename(); // 创建上传请求 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file.getInputStream(), null); // 执行上传操作 PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); // 关闭COSClient cosClient.shutdown(); return "文件上传成功,COS对象键:" + putObjectResult.getKey(); } catch (IOException e) { e.printStackTrace(); } return "文件上传失败"; } } ``` 以上示例代码中,`cos.secretId`、`cos.secretKey`、`cos.bucketName`和`cos.region`分别对应COS的SecretId、SecretKey、Bucket名称和Bucket所在的地域。你需要将这些值替换为你自己的配置。 4. 启动Spring Boot应用程序,并通过POST请求将文件发送到`/upload`路由。 这样,你就可以通过Spring Boot将文件上传COS了。请确保已正确配置COS的访问权限和相关配置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值