后端笔记之tegg初试

tegg初次尝试以及写一个后端接收文件上传的接口

1.tegg

再一次用脚手架构建egg项目,发现写法全然不同,懵逼了半天才搞懂了这些注解,呸,装饰器的含义,先附上我的学习路径
tegg的github
tegg装饰器详解
首先download下egg项目之后,目录如下
在这里插入图片描述

2.写一个文件上传接收的接口,用于前端测试

由于本接口只是用于前端测试,所以文件就直接写入了项目本地,所以要在config里面提前声明项目静态文件存放地址, 打开congfig.default增加以下配置。
在这里插入图片描述
bar下面controller下新建file.ts,
代码如下

import { EggLogger, EggAppConfig } from 'egg';
import { Context, EggContext, HTTPController, HTTPMethod, HTTPMethodEnum, Inject } from '@eggjs/tegg';
import { FileService } from '@/module/foo';
import * as fs from 'fs';
import { mkdirp } from 'mkdirp';
import path from 'path';


@HTTPController({
  path: '/file',
})
export class file {
  @Inject()
  logger: EggLogger;
  @Inject()
  config: EggAppConfig;
  @Inject()
  fileService: FileService;

  @HTTPMethod({
    method: HTTPMethodEnum.POST,
    path: '/singleFileUpload',
  })
  async singleFileUpload(@Context() ctx: EggContext) {
    const file = ctx.request.files[0];
    let uploadDir = '';
    try {
      const f = fs.readFileSync(file.filepath);
      const day = new Date().getFullYear() + '-' + new Date().getMonth();
      const dir = path.join(this.config.uploadDir, day);
      await mkdirp(dir);
      uploadDir = path.join(dir, file.filename);
      fs.writeFileSync(uploadDir, f);
      const filePath = uploadDir.replaceAll('app\\', '');
      return { code: 200, data: { src: filePath } };
    } catch (e) {
      return { code: 500, msg: e };
    } finally {
      await ctx.cleanupRequestFiles();
    }
  }
}

然后,就完了呀。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.io.*; import javax.swing.*; public class diyiti1 { private JFrame frame; public static void main(String[] args) { diyiti1 that=new diyiti1(); that.go(); } public void go(){ int counta=0,countb=0,countc=0,countd=0,counte=0; JButton tapple,tbanana,tcat,tdog,tegg; FileInputStream a; String oldStr = ""; try { a = new FileInputStream("E:\\a.txt"); InputStreamReader br = new InputStreamReader(a); BufferedReader reader = new BufferedReader(br); String line = null; while((line = reader.readLine())!=null){ oldStr += line; } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } char array[]=new char[oldStr.length()]; for(int j=0;j<5;j++) array[j]=oldStr.charAt(j); for(String c:){ switch(c){ case "apple":counta++;break; case "banana":countb++;break; case "cat":countc++;break; case "dog":countd++;break; case "egg":counte++;break; default:System.out.print("Wrong!"); } } frame=new JFrame("统计"); Container content=frame.getContentPane(); content.setLayout(null); tapple=new JButton("apple的个数"+counta); tbanana=new JButton("banana的个数"+countb); tcat=new JButton("cat的个数"+countc); tdog=new JButton("dog的个数"+countd); tegg=new JButton("egg的个数"+counte); //显示 tapple.setBounds(1,0,counta*100,20); tbanana.setBounds(1,20,countb*100,20); tcat.setBounds(1,40,countc*100,20); tdog.setBounds(1,60,countd*100,20); tegg.setBounds(1,80,counte*100,20); //图像 tapple.setBackground(Color.yellow); tbanana.setBackground(Color.red); tcat.setBackground(Color.blue); tdog.setBackground(Color.green); tegg.setBackground(Color.white); //botton颜色 content.add(tapple); content.add(tbanana); content.add(tcat); content.add(tdog); content.add(tegg); // frame.setSize(300,200); frame.setVisible(true); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值