字段和属性的意思,static的作用,static string path;这句代码中static的所用
时间: 2024-06-05 09:13:07 浏览: 125
在面向对象编程中,一个类包含了类名和数据(字段)以及方法(属性),其中字段是指一个类中的变量,属性是指与变量相关联的方法。字段用于存储对象的状态信息,而属性则用于控制对象状态的访问方式。
static是C++中的一个关键字,表示静态的、不变的,使用static修饰的成员变量和成员函数属于整个类,而不是属于类的某个对象。当static修饰成员变量时,该变量被所有类的对象所共享,而不是每个对象都拥有一份独立的拷贝;当static修饰成员函数时,该函数可以直接通过类名调用,无需创建对象,因为它不需要访问任何特定对象的状态。
在代码中,static string path;表示在类中定义了一个静态的字符串变量path,该变量被所有类的对象所共享,可以通过类名直接访问。这样做的好处是可以在不创建对象的情况下访问和修改该变量,从而节省内存空间。
相关问题
<h1>上传/下载文件</h1> <form method="post" action="/file/upload" enctype="multipart/form-data"> <input type="file" name="file" id="fileInput"/> <input type="submit" value="上传" /> </form> <hr> <form method="post" action="/file/download" > <input type="text" name="fileName" placeholder="请输入文件名" /> <input type="submit" value="下载" id="downloadBtn" /> </form> public class SFTPUtil { private static final String host = "10.60.0.160"; private static final int port = 22; private static final String username = "root"; private static final String password = "Z20"; private static Session session = null; public static void connect() throws Exception { } public static void upload(MultipartFile file, String path, String fileName) throws Exception { } public static boolean download(HttpServletResponse response, String path, String fileName) throws Exception { } @Controller @RequestMapping("/file") public class FileController { private static final String UPLOAD_PATH = "/data/sftp/adtuser1/upload/winbidding/"; private static final String DOWNLOAD_PATH = "/data/sftp/adtuser1/upload/winbidding/"; @PostMapping("/upload") @ResponseBody public ModelAndView upload(@RequestParam("file") MultipartFile file) { } @PostMapping("/download") public ModelAndView download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws Exception { } 数据库有host port username password UPLOAD_PATH DOWNLOAD_PATH csmc字段,把上边写死的改成数据库查询,前台页面增加csmc字段下拉框,根据csmc字段把查出来的配置数据完成上传下载功能,用的mysql数据库,把代码改好写出来
首先需要在数据库中创建一个表,用于存储SFTP连接的相关信息:
```sql
CREATE TABLE sftp_config (
id INT PRIMARY KEY AUTO_INCREMENT,
csmc VARCHAR(50),
host VARCHAR(50),
port INT,
username VARCHAR(50),
password VARCHAR(50),
upload_path VARCHAR(255),
download_path VARCHAR(255)
);
```
然后在Spring Boot项目中创建一个SFTPUtil类,用于实现SFTP上传和下载功能:
```java
import com.jcraft.jsch.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Properties;
public class SFTPUtil {
private String host;
private int port;
private String username;
private String password;
private String uploadPath;
private String downloadPath;
private Session session;
public SFTPUtil(String host, int port, String username, String password, String uploadPath, String downloadPath) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.uploadPath = uploadPath;
this.downloadPath = downloadPath;
}
public void connect() throws Exception {
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
public void upload(MultipartFile file, String fileName) throws Exception {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd(uploadPath);
InputStream inputStream = file.getInputStream();
channel.put(inputStream, fileName);
inputStream.close();
channel.disconnect();
}
public boolean download(HttpServletResponse response, String fileName) throws Exception {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd(downloadPath);
SftpATTRS attrs = channel.lstat(fileName);
if (attrs == null) {
channel.disconnect();
return false;
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream outputStream = response.getOutputStream();
channel.get(fileName, outputStream);
outputStream.flush();
outputStream.close();
channel.disconnect();
return true;
}
}
```
然后在Spring Boot项目中创建一个FileController类,用于处理文件上传和下载请求:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private JdbcTemplate jdbcTemplate;
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file, @RequestParam("csmc") String csmc) {
try {
// 从数据库中获取SFTP连接配置信息
String sql = "SELECT * FROM sftp_config WHERE csmc = ?";
Map<String, Object> map = jdbcTemplate.queryForMap(sql, csmc);
String host = (String) map.get("host");
int port = (int) map.get("port");
String username = (String) map.get("username");
String password = (String) map.get("password");
String uploadPath = (String) map.get("upload_path");
String fileName = file.getOriginalFilename();
// 使用SFTP上传文件
SFTPUtil sftpUtil = new SFTPUtil(host, port, username, password, uploadPath, null);
sftpUtil.connect();
sftpUtil.upload(file, fileName);
return "上传成功";
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}
@PostMapping("/download")
public void download(@RequestParam("fileName") String fileName, @RequestParam("csmc") String csmc, HttpServletResponse response) throws Exception {
// 从数据库中获取SFTP连接配置信息
String sql = "SELECT * FROM sftp_config WHERE csmc = ?";
Map<String, Object> map = jdbcTemplate.queryForMap(sql, csmc);
String host = (String) map.get("host");
int port = (int) map.get("port");
String username = (String) map.get("username");
String password = (String) map.get("password");
String downloadPath = (String) map.get("download_path");
// 使用SFTP下载文件
SFTPUtil sftpUtil = new SFTPUtil(host, port, username, password, null, downloadPath);
sftpUtil.connect();
boolean result = sftpUtil.download(response, fileName);
if (!result) {
response.getWriter().write("文件不存在");
}
}
@GetMapping("/csmcList")
@ResponseBody
public List<String> getCsmcList() {
// 从数据库中获取所有的csmc字段
String sql = "SELECT DISTINCT csmc FROM sftp_config";
List<String> csmcList = jdbcTemplate.queryForList(sql, String.class);
return csmcList;
}
}
```
最后,在HTML页面中加入下面的代码,即可实现文件上传和下载:
```html
<h1>上传/下载文件</h1>
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="file" id="fileInput"/>
<select name="csmc" id="csmcSelect"></select>
<input type="submit" value="上传"/>
</form>
<hr>
<form method="post" action="/file/download">
<input type="text" name="fileName" placeholder="请输入文件名"/>
<select name="csmc" id="csmcSelect"></select>
<input type="submit" value="下载" id="downloadBtn"/>
</form>
<script>
// 获取csmc列表
$.get("/file/csmcList", function (data) {
var csmcSelect = $("#csmcSelect");
for (var i = 0; i < data.length; i++) {
var option = $("<option>").val(data[i]).text(data[i]);
csmcSelect.append(option);
}
});
</script>
```
涉及到CredentialCache的代码是: internal class ErrorStationHandler { private static readonly string FILE_PATH = @"\\111.11.111.11\共享\error_station\error_stations.txt"; private static readonly string FILE_ZD = @"\111.11.111.11\共享\error_station\fileZD.txt"; private readonly Dictionary<string, int> stationErrorTypes = new Dictionary<string, int>(); private readonly StringBuilder sb = new StringBuilder(); private readonly CredentialCache _cache; public ErrorStationHandler(CredentialCache cache) { _cache = cache; stationErrorTypes = LoadDictionary(FILE_ZD); }
根据您提供的代码,`ErrorStationHandler` 类的构造函数接受一个 `CredentialCache` 类型的参数,并将其赋值给私有字段 `_cache`。在代码中,构造函数还调用了 `LoadDictionary` 方法来加载错误类型字典。
如果您想要使用 `ErrorStationHandler` 类的实例来调用 `WriteErrorStationsToTxt` 方法,您需要修改代码以创建 `ErrorStationHandler` 对象的实例,并将 `CredentialCache` 对象作为参数传递给构造函数。
以下是一个示例:
```csharp
CredentialCache cache = new CredentialCache();
// 根据需要设置 cache 的属性
ErrorStationHandler errorStationHandler = new ErrorStationHandler(cache);
errorStationHandler.WriteErrorStationsToTxt(tyMonitors);
```
请确保在使用 `WriteErrorStationsToTxt` 方法之前创建了 `ErrorStationHandler` 对象,并将正确的 `CredentialCache` 对象传递给构造函数。
如果还有其他问题,请提供更多关于代码结构和上下文的信息,以便我可以提供更具体的帮助。
阅读全文