parent
fa04f59888
commit
fc7daeee79
13 changed files with 380 additions and 137 deletions
@ -0,0 +1,66 @@ |
||||
package com.teaching.backend.controller; |
||||
|
||||
import com.teaching.backend.utils.WordUtil; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.ResponseEntity; |
||||
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.RestController; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
@RestController |
||||
public class WordController { |
||||
private String outputPath; |
||||
|
||||
@PostMapping("/Word/get") |
||||
public ResponseEntity<byte[]> createWord(@RequestParam("outputPath") String outputPath) { |
||||
String filename = "generatedWord.docx"; |
||||
System.out.println(outputPath); |
||||
WordUtil wordUtil = new WordUtil(); |
||||
// 设置模板文件存放的目录
|
||||
wordUtil.setBaseDir("C:\\Users\\jian\\Desktop\\123345\\"); |
||||
// 设置模板文件名称
|
||||
wordUtil.setTemplateFile("123.ftl"); |
||||
// 设置 Word 生成的输出目录
|
||||
wordUtil.setOutputDir(outputPath); |
||||
|
||||
Map<String, Object> dataMap = new HashMap<>(); |
||||
dataMap.put("username1", "姜钧瀚"); |
||||
|
||||
|
||||
|
||||
String generatedFilePath = wordUtil.createWord(dataMap); |
||||
if (generatedFilePath.equals("操作失败")) { |
||||
System.out.println("操作失败"); |
||||
return ResponseEntity.badRequest().body("操作失败".getBytes()); |
||||
} |
||||
|
||||
// 读取生成的 Word 文件内容
|
||||
File file = new File(generatedFilePath); |
||||
byte[] fileContent; |
||||
try { |
||||
fileContent = Files.readAllBytes(file.toPath()); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
return ResponseEntity.badRequest().body("文件读取失败".getBytes()); |
||||
} |
||||
|
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
||||
headers.setContentDispositionFormData("attachment", filename); |
||||
|
||||
//返回word文件
|
||||
return new ResponseEntity<>(fileContent, headers, HttpStatus.OK); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,68 @@ |
||||
package com.teaching.backend.utils; |
||||
|
||||
import com.aliyun.oss.ClientException; |
||||
import com.aliyun.oss.OSS; |
||||
import com.aliyun.oss.OSSClientBuilder; |
||||
import com.aliyun.oss.OSSException; |
||||
import com.aliyun.oss.common.auth.CredentialsProviderFactory; |
||||
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider; |
||||
import com.aliyun.oss.model.PutObjectRequest; |
||||
import com.aliyun.oss.model.PutObjectResult; |
||||
|
||||
import java.awt.*; |
||||
import java.io.FileInputStream; |
||||
import java.io.InputStream; |
||||
|
||||
public class AliOssTest { |
||||
public static void main(String[] args) throws Exception { |
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。因为是华中地区 所以需要加上-lr
|
||||
String endpoint = "oss-cn-wuhan-lr.aliyuncs.com"; |
||||
|
||||
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
|
||||
// EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
String accessKeyId ="LTAI5tFkdu3y5WddxbjgaG2F"; |
||||
String accessKeySecret ="1xUchxUTlmUBoTV5JQIrKsVjSkmsLF"; |
||||
|
||||
// 填写Bucket名称,例如examplebucket。
|
||||
String bucketName = "ceshi132132"; |
||||
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
|
||||
String objectName = "ceshi/33.docx"; |
||||
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。
|
||||
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||
String filePath= "C:\\Users\\jian\\Desktop\\123345\\999\\33.docx"; |
||||
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
||||
// OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
|
||||
|
||||
|
||||
try { |
||||
InputStream inputStream = new FileInputStream(filePath); |
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream); |
||||
// 设置该属性可以返回response。如果不设置,则返回的response为空。
|
||||
putObjectRequest.setProcess("true"); |
||||
// 创建PutObject请求。
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest); |
||||
// 如果上传成功,则返回200。
|
||||
System.out.println(result.getResponse().getStatusCode()); |
||||
} catch (OSSException oe) { |
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, " |
||||
+ "but was rejected with an error response for some reason."); |
||||
System.out.println("Error Message:" + oe.getErrorMessage()); |
||||
System.out.println("Error Code:" + oe.getErrorCode()); |
||||
System.out.println("Request ID:" + oe.getRequestId()); |
||||
System.out.println("Host ID:" + oe.getHostId()); |
||||
} catch (ClientException ce) { |
||||
System.out.println("Caught an ClientException, which means the client encountered " |
||||
+ "a serious internal problem while trying to communicate with OSS, " |
||||
+ "such as not being able to access the network."); |
||||
System.out.println("Error Message:" + ce.getMessage()); |
||||
} finally { |
||||
if (ossClient != null) { |
||||
ossClient.shutdown(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,84 @@ |
||||
package com.teaching.backend.utils; |
||||
|
||||
import freemarker.template.Configuration; |
||||
import freemarker.template.Template; |
||||
import freemarker.template.TemplateException; |
||||
import lombok.Data; |
||||
|
||||
|
||||
import java.io.*; |
||||
import java.net.URLDecoder; |
||||
import java.util.Map; |
||||
import java.util.Random; |
||||
|
||||
/** |
||||
* @Author:youhang |
||||
* @Date:2024-05-30-18:20 |
||||
* @Description: |
||||
*/ |
||||
@Data |
||||
public class WordUtil { |
||||
|
||||
private Configuration configuration = null; |
||||
|
||||
/* |
||||
* 模板文件存放的目录 |
||||
*/ |
||||
private String baseDir; |
||||
|
||||
/* |
||||
* 模板文件名称 |
||||
*/ |
||||
private String templateFile; |
||||
|
||||
/* |
||||
* word生成的输出目录 |
||||
*/ |
||||
private String outputDir; |
||||
|
||||
public WordUtil(){ |
||||
configuration = new Configuration(); |
||||
configuration.setDefaultEncoding("UTF-8"); |
||||
} |
||||
|
||||
/* |
||||
* <p>转换成word<br> |
||||
*/ |
||||
public String createWord(Map<String,Object> dataMap){ |
||||
|
||||
configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径
|
||||
|
||||
Template t = null; |
||||
|
||||
try { |
||||
//得到模板文件
|
||||
configuration.setDirectoryForTemplateLoading(new File(baseDir)); |
||||
t = configuration.getTemplate(templateFile); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
//随机生成
|
||||
Random random=new Random(); |
||||
|
||||
File outFile = new File(outputDir+ random.nextInt(200) + ".docx"); //导出文件
|
||||
|
||||
Writer out = null; |
||||
try { |
||||
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); |
||||
} catch (FileNotFoundException e1) { |
||||
e1.printStackTrace(); |
||||
} |
||||
try { |
||||
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
|
||||
return outFile.getPath(); |
||||
} catch (TemplateException e) { |
||||
e.printStackTrace(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return "文档生成失败"; |
||||
} |
||||
|
||||
|
||||
} |
@ -1,9 +0,0 @@ |
||||
package com.teaching.backend.utils; |
||||
|
||||
/** |
||||
* @Author:youhang |
||||
* @Date:2024-05-30-18:20 |
||||
* @Description: |
||||
*/ |
||||
public class test1 { |
||||
} |
@ -1,5 +1,5 @@ |
||||
teaching-backend.img.path=C:\\Users\\jian\\Desktop\\cc\\teaching-backend\\src\\main\\resources\\static\\ |
||||
teaching-backend.video.path=C:\\Users\\jian\\Desktop\\cc\\teaching-backend\\src\\main\\resources\\static\\video\\ |
||||
spring.servlet.multipart.max-file-size=10MB |
||||
spring.servlet.multipart.max-request-size=10MB |
||||
aliyun.oss.endpoint=oss-cn-wuhan-lr.aliyuncs.com |
||||
aliyun.oss.accessKeyId=LTAI5tFkdu3y5WddxbjgaG2F |
||||
aliyun.oss.accessKeySecret=1xUchxUTlmUBoTV5JQIrKsVjSkmsLF |
||||
aliyun.oss.bucketName=ceshi132132 |
||||
|
||||
|
Loading…
Reference in new issue