「java本地缓存内存分配」java中的内存分配

博主:adminadmin 2022-12-17 08:06:05 71

今天给各位分享java本地缓存内存分配的知识,其中也会对java中的内存分配进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何将对象暂存到内存中?

form表单提交文件,建议用smartupload上传,暂存在web服务器目录下,然后稍微一下下面的代码,ftp上传后,删除暂存文件,ok

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.StringTokenizer;

import org.apache.commons.net.;

import org.apache.commons.net.;

import org.apache.commons.net.;

import org.apache.log4j.Logger;

/**

* Ftp 服务类,对Apache的commons.net.ftp进行了包装br

* 依赖库文件:commons-net-1.4.1.jar

*

* @version 1.0 2008-02-18

* @author huchao@jbsoft

*/

public class FtpService {

public FtpService(String serverAddr, String lsenport, String userName,

String pswd) {

this.ftpServerAddress = serverAddr;

this.port = Integer.parseInt(lsenport);

this.user = userName;

this.password = pswd;

}

/**

* FTP 服务器地址

*/

private String ftpServerAddress = null;

/**

* FTP 服务端口

*/

private int port = 21;

/**

* FTP 用户名

*/

private String user = null;

/**

* FTP 密码

*/

private String password = null;

/**

* FTP 数据传输超时时间

*/

private int timeout = 0;

/**

* 异常:登录失败

*/

private final I2HFException EXCEPTION_LOGIN = new I2HFException("COR101",

"FTP服务器登录失败");

/**

* 异常:文件传输失败

*/

private final I2HFException EXCEPTION_FILE_TRANSFER = new I2HFException(

"COR010", "FTP文件传输失败");

/**

* 异常:IO异常

*/

private final I2HFException EXCEPTION_GENERAL = new I2HFException("COR010",

"FTP IO 异常");

private static final Logger logger = Logger.getLogger(FtpService.class);

/**

* 初始化FTP连接,并进行用户登录

*

* @return FTPClient

* @throws I2HFException

*/

public FTPClient initConnection() throws I2HFException {

FTPClient ftp = new FTPClient();

try {

// 连接到FTP

(ftpServerAddress, port);

int reply = ;

if (!FTPReply.isPositiveCompletion(reply)) {

;

throw new I2HFException("COR010", "FTP服务器连接失败");

}

// 登录

if (!(user, password)) {

throw EXCEPTION_LOGIN;

}

// 传输模式使用passive

;

// 设置数据传输超时时间

;

logger.info("FTP服务器[" + ftpServerAddress + " : " + port + "]登录成功");

} catch (I2HFException te) {

logger.info(te.errorMessage, te);

throw te;

} catch (IOException ioe) {

logger.info(ioe.getMessage(), ioe);

throw EXCEPTION_LOGIN;

}

return ftp;

}

/**

* 设置传输方式

*

* @param ftp

* @param binaryFile

* true:二进制/false:ASCII

* @throws I2HFException

*/

public void setTransferMode(FTPClient ftp, boolean binaryFile)

throws I2HFException {

try {

if (binaryFile) {

;

logger.info("FTP文件传输方式为:二进制");

} else {

;

logger.info("FTP文件传输方式为:ASCII");

}

} catch (IOException ex) {

logger.info(ex.getMessage(), ex);

throw EXCEPTION_GENERAL;

}

}

/**

* 在当前工作目录下建立多级目录结构

*

* @param ftp

* @param dir

* @throws I2HFException

*/

public void makeMultiDirectory(FTPClient ftp, String dir)

throws I2HFException {

try {

StringBuffer fullDirectory = new StringBuffer();

StringTokenizer toke = new StringTokenizer(dir, "/");

while (toke.hasMoreElements()) {

String currentDirectory = (String) toke.nextElement();

fullDirectory.append(currentDirectory);

(fullDirectory.toString());

if (toke.hasMoreElements()) {

fullDirectory.append('/');

}

}

} catch (IOException ex) {

logger.info(ex.getMessage(), ex);

throw EXCEPTION_GENERAL;

}

}

/**

* 更改服务器当前路径

*

* @param ftp

* @param dir

* @throws I2HFException

*/

public void changeWorkingDirectory(FTPClient ftp, String dir)

throws I2HFException {

try {

if (!) {

throw new I2HFException("COR010", "目录[ " + dir + "]进入失败");

}

} catch (I2HFException tfe) {

logger.info(tfe.errorMessage, tfe);

throw tfe;

} catch (IOException ioe) {

logger.info(ioe.getMessage(), ioe);

throw EXCEPTION_GENERAL;

}

}

/**

* 上传文件到FTP服务器

*

* @param ftp

* @param localFilePathName

* @param remoteFilePathName

* @throws I2HFException

*/

public void uploadFile(FTPClient ftp, String localFilePathName,

String remoteFilePathName) throws I2HFException {

InputStream input = null;

try {

input = new FileInputStream(localFilePathName);

boolean result = (remoteFilePathName, input);

if (!result) {

// 文件上传失败

throw EXCEPTION_FILE_TRANSFER;

}

logger.info("文件成功上传到FTP服务器");

} catch (I2HFException tfe) {

logger.info(tfe.getMessage(), tfe);

throw tfe;

} catch (IOException ioe) {

logger.info(ioe.getMessage(), ioe);

throw EXCEPTION_FILE_TRANSFER;

} finally {

try {

if (input != null) {

input.close();

}

} catch (IOException ex) {

logger.info("FTP对象关闭异常", ex);

}

}

}

/**

* 下载文件到本地

*

* @param ftp

* @param remoteFilePathName

* @param localFilePathName

* @throws I2HFException

*/

public void downloadFile(FTPClient ftp, String remoteFilePathName,

String localFilePathName) throws I2HFException {

boolean downloadResult = false;

OutputStream output = null;

try {

output = new FileOutputStream(localFilePathName);

downloadResult = (remoteFilePathName, output);

if (!downloadResult) {

// 如果是文件不存在将异常抛出

throw new I2HFException("COR011", "文件不存在");

}

logger.info("文件成功从FTP服务器下载");

} catch (I2HFException tfe) {

logger.error(tfe.getMessage(), tfe);

throw tfe;

} catch (IOException ex) {

logger.error(ex.getMessage(), ex);

throw EXCEPTION_FILE_TRANSFER;

} finally {

try {

if (output != null) {

output.close();

}

if (!downloadResult) {

new File(localFilePathName).delete();

}

} catch (IOException ex) {

logger.error("FTP对象关闭异常", ex);

}

}

}

/**

* Method setFtpServerAddress.

*

* @param ftpServerAddress

* String

*/

public void setFtpServerAddress(String ftpServerAddress) {

this.ftpServerAddress = ftpServerAddress;

}

/**

* Method setUser.

*

* @param user

* String

*/

public void setUser(String user) {

this.user = user;

}

/**

* Method setPassword.

*

* @param password

* String

*/

public void setPassword(String password) {

this.password = password;

}

/**

* Method setTimeout.

*

* @param timeout

* String

*/

public void setTimeout(String timeout) {

try {

this.timeout = Integer.parseInt(timeout);

} catch (NumberFormatException ex) {

// 默认超时时间500毫秒

this.timeout = 500;

}

}

/**

* Method setPort.

*

* @param port

* String

*/

public void setPort(String port) {

try {

this.port = Integer.parseInt(port);

} catch (NumberFormatException ex) {

// 默认端口21

this.port = 21;

}

}

}

=====================================

jsp上传部分

===================================

form method="post" name="uploadform" id="uploadform" action="upload" ENCTYPE="multipart/form-data"

input type="hidden" name="service" value="com.jbsoft.i2hf.corpor.services.CstBatchBizServices.exclUpload"/

table cellspacing="0" cellpadding="0"

tr

td width="20%" align="right"上传本地文件:/td

td width="80%"input class="" style="width:300px" type="file" width="300px" name="exclname" id="exclname"//td

/tr

/table

/form

============================================

上传的servlet用的是smartupload

,部分代码可以参考一下:

==========================================

SmartUpload su = new SmartUpload();

su.setCharset("UTF-8");

su.initialize(getServletConfig(), request, response);

su.setMaxFileSize(10240000);

su.setTotalMaxFileSize(102400000);

su.setAllowedFilesList("xls");

su.upload();

===========================================

代码里面有一些客户的信息,不能全部给你哈

java中i++操作是不是从本地内存取值到寄存器,计算后赋给本地内存?刷新会主内存的时间是不是不确定的?

通常来说,当i是普通临时变量时,其分配位于程序栈中,i++对应的计算机的操作是,将i的值由栈中取到寄存器,实现加1,然后再存回到栈中i对应位置

至于所谓刷新主内存,涉及到计算机cpu利用cache或者说缓存访问外部存储器,程序堆栈开设在外部存储器时,cpu为提高访问速度和效率,将外存整块读入cpu的cache,临时操作在cache中进行,当系统判定需要访问其他存储区或者要求将保存的数据刷新到片外时,会调用功能完成。这种刷新一般不由像java这样的程序来控制。

java 怎么把数据存到内存中

这里你采纳与否没关系,给你说说编程与内存的关系。

你定义的任何变量,常量,类,方法等等,其实都在内存中,没有所谓的把数据存内存中,这概念,你可以想一下电脑重启或关机后,内存中的所有数据,都会丢失,除非你保存到磁盘中去。

在内存中的数据有两块,第一、缓冲,一般写数据到磁盘的时候开辟出来的内存空间;第二、缓存,一般是从磁盘读数据到内存中开辟出来的内存空间。会这么使用,原因很简单,磁盘读写数据速度与内存不一致(磁盘的存取效率远远小于内存的存取效率),为了提高数据的存取效率,才会这么干的。

一般而言,java中的所谓数据,大部分都是类,从自动引用计数的概念来分析,你想把对象长久的放在内存中,不会被垃圾回收机制释放,注意制药有一个对象在使用/引用你的数据,这条数据就会存在内存中。所以,想servlet中的全局配置参数,随时可以取到还是唯一一份,你可以参考一下。

另外内存使用分堆与栈,堆在面向对象编程中存储对象的,栈是方法或函数执行的时候临时开辟的存储空间,方法或函数执行完毕就会释放。

希望我的回复能帮助到你,采纳与否没关系。有更好的答案,我就隐藏我的回复。

问一个java并发方面的问题,java线程中的本地内存(缓存,高速缓存,寄存器等)是批量刷新的吗

你的4个变量一旦声明会自动在内存中创建。

jvm也没有你说的缓存,高速缓存这些东西,全部放在堆内存中。

堆内存中的东西在GC的时候会移动

关于java本地缓存内存分配和java中的内存分配的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-17,除非注明,否则均为首码项目网原创文章,转载请注明出处。