有两部操作
第一步点击按钮后,程序将一个文件从本地读取,转换成流,写入内存,写入成功后跳转页面。第二部在跳转后的页面,点击按钮,程序从内存取出刚才存入的流,写成文件,存入本地。请问后台的代码改怎么写呀?????
第一步点击按钮后,程序将一个文件从本地读取,转换成流,写入内存,写入成功后跳转页面。第二部在跳转后的页面,点击按钮,程序从内存取出刚才存入的流,写成文件,存入本地。请问后台的代码改怎么写呀?????
解决方案 »
- 关于关键字输出的问题 就比如***
- 递归问题!
- 新手请教 我的第二个sql错在哪里 总被catch(SQLException e)捉到
- JInternalFrame的大问题,急急急!!!!!!
- 请教java中使用common-net-ftp,中文文件名和目录的问题!!!
- 请问 html 的<marquee>标记能应用于jLabel 中吗?~~~
- 大家帮帮新手!一共80分,另两贴拿!
- 如何设置Date类的显示格式为"1998-09-01 12:02:33"这种格式!
- 300分大赠送,简单,同志们进来看看!问题搞定后,再开两个贴加200分!
- 一个有关磁盘操作的问题
- 设计模式
- 一個關於JDBC的問題
使用PrintWriter写文件就可以了
我之前的想法是 将文件读入 File对象
然后页面跳转时 放到表单中 之后再从表单取出来
不知道行不行
第一个页面,把前台的文件通过流读取到byte数组,后台保存,第一个页面把byte数组的数据写入到文件
try{in = new FileInputStream(file);
in2 = new ObjectInputStream(in);
TextArea temp = (TestArea)in2.readObject();
....}catch{....}
if(e.getSource)==写出)
try{out = new FileOutputStream(file);
out2 = new ObjectOutputStream(out);
out2.writeObject(text);
....
}catch{...}
InputStream,OutputStream这2个是基类。你需要源码?
你是问具体的上传操作么?你可以将上传文件读入到内存输出流中保存(ByteArrayOutputStream) 然后在触发写成文件的操作的时候再从内存输出流中获取刚刚输入的流 再写到指定文件上不过最好不要保存到内存中 你可以在上传之后就写到一个临时文件里 然后再保存的时候再通过shell复制并重命名到你指定的位置
如果是稍微大一点的文件,解决方案就错了。你最好把实际的问题写出来。
首先在服务器端配置一个memcache,上传后将文件内容写入到memcache,下一个页面再从memcache中读出,这样文件内容始终是在内存里面。
memcache的用法去google上搜一下。
JAVA 的IO就那么几个东西。
用FileChannel读取到ByteBuffer缓冲区,然后再用FileChannel写入到文件
import java.io.File;
import java.io.FileOutputStream;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class TuPian{
public void jpgTset() throws Exception{
File _file = new File("d:\\Project\\Test\\pic\\1\\1.jpg"); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int wideth=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
System.out.println("长度为"+String.valueOf(wideth));
System.out.println("宽度为"+String.valueOf(height));
BufferedImage tag = new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,wideth/2,height/2,null); //绘制缩小后的图
// 输出到文件流
FileOutputStream out=new FileOutputStream("d:\\Project\\Test\\pic\\2\\newfile.jpg");
// File file = new File("newFile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //JPEG编码
out.close();
}
public static void main(String[] args){ try{
new TuPian().jpgTset();
}catch(Exception e){
e.printStackTrace();
}
}
}
String url = "D:\\test.txt";
FileInputStream fis = new FileInputStream(url);
File file = new File(fis); //其实可以不用上面一行,直接 File file = new File(url);从内存中读取并存放到硬盘中的文件:(其实就是从上面已经获得的fis中读取)
File destFile = new File(destPath);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buf = new byte[1024];
int c;
while ((c = fis.read(buf)) != -1) {
fos.write(buf, 0, c);
}
fis.close();
fos.close();
所以就在代码中标记了一下时间 这样记录的还准确
谢谢各位~~~~~