Flask接口签名sign原理与实例代码浅析
256
2023-06-03
ssh框架实现文件上传下载实例代码
最近在学习ssh框架,我用ssh框架写了一个文件上传下载的实例代码,有需要了解的朋友可参考。希望此文章对各位有所帮助。
&dORZvRIlt;/head>
package com.cpsec.tang.chemical.action;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller("newFileAction")
public class NewFileAction extends ActionSupport {
private File file;//文件
private String fileFileName;//文件名称
private String fileContentType; //文件类型
private String filename;
private InputStream inputStream;
public String newFile(){
File dir=new File(ServletActionContext.getServletContext().getRealPath("files"));
//判断文件是否上传,如果上传的话将会创建该目录
if(!dir.exists()){
dir.mkdir(); //创建该目录
}
System.out.println(file);
System.out.println(fileFileName);
//第一种文件上传的方法
//声明文件输入流,为输入流指定文件路径
//获取输出流,获取文件的文件地址及名称
FileInputStream in=null;
FileOutputStream out=null;
try{
in= new FileInputStream(file);
out=new FileOutputStream(dir + "\\" +fileFileName);
byte[] b=new byte[1024*1024];//每次写入的大小
int i=0;
while((i=in.read(b))>0){
out.write(b,0,i);
}
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}finally{
}
/*//第二种文件上传的方法
try {
FileUtils.copyFile(file,new File(dir,fileFileName));
} catch (IOException e) {
e.printStackTrace();
}
//第三种方法
BufferedReader bReader=null;
BufferedWriter bWriter=null;
try{
bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
bWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir+"\\"+fileFileNdORZvRIame)));
char[] str=new char[1024*1024];
int i=0;
while((i=bReader.read(str))>0){
bWriter.write(str,0,i);
}
bReader.close();
bWriter.close();
dir.delete();
}catch(Exception e){
e.printStackTrace();
}finally{
} */
return SUCCESS;
}
public String downFile(){
System.out.println(filename);
inputStream=ServletActionContext.getServletContext().getResourceAsStream("/files/"+filename);
System.out.println(inputStream);
return SUCCESS;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public InputStream getInputStream() {
return inputStreadORZvRIm;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
application/octet-stream;charset=ISO8859-1
inputStream
attachment;filename=${filename}
4096
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~