ssh中如何文件上传

注意:文件的上传和下载最好放到不同的文件夹中,本实例上传文件夹名称为shangchuan    下载文件夹名称为  xiazai
############################单文件上传##################################
1、导入jar包
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
2、创建jsp页面确保form标签中有 enctype="multipart/form-data"属性
<form action="upload.action" method="post" enctype="multipart/form-data"
name="form1" id="form1">
<p>
照片:
<input type="file" name="photo" id="fileField" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>
3、编写文件上传处理Action


public class UploadAction extends ActionSupport {
private String userName;
// 封装上传文件属性
private File photo;
// 获取提交的文件类型
private String photoContentType;
// 封装上传文件名称
private String photoFileName;
//有struts.xml中通过param标签指定的上传路径
private String uploadPath;


@Override
public String execute() throws Exception {
// 控制文件类型
// System.out.println(this.getPhotoContentType());
// if(this.getPhotoContentType().indexOf("image")==0){
//
// }


byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(this.getPhoto());
String path = ServletActionContext.getServletContext().getRealPath(
this.getUploadPath());
// 创建一个文件夹
FileUtil.createDic(path, FileUtil.getDateString());


FileOutputStream fos = new FileOutputStream(path + "\\"
+ FileUtil.getDateString() + "\\" + this.getPhotoFileName());
System.out.println("要上传的路径为:" + path + "\\" + FileUtil.getDateString()
+ "\\" + this.getPhotoFileName());
int length = fis.read(buffer);
// 控制文件上传大小
// System.out.println("文件大小:" + fis.available());
// if (fis.available() <= 1024 * 100) {
//
// }


while (length > 0) {
// 每次写入length长度的内容
fos.write(buffer, 0, length);
length = fis.read(buffer);
}
fis.close();
fos.flush();
fos.close();
// 图片加水印
// ImageRemarkUtil.markImageByText("***商城",path+"\\"+this.getPhotoFileName(),path+"\\"+this.getPhotoFileName(),-45);
return SUCCESS;
}


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public File getPhoto() {
return photo;
}


public void setPhoto(File photo) {
this.photo = photo;
}


public String getPhotoContentType() {
return photoContentType;
}


public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}


public String getPhotoFileName() {
String ext = FilenameUtils.getExtension(photoFileName);// 获取后缀名
return photoFileName = UUID.randomUUID().toString() + "." + ext;
}


public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}

public String getUploadPath() {
return uploadPath;
}


public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
}
4、在struts.xml中配置Action
<action name="upload" class="action.UploadAction">
<!-- 通过设置action默认参数指定上传路径 -->
<param name="uploadPath">/shangchuan</param>
<result>show.jsp</result>
</action>


############################多文件上传##################################
1、导入jar包
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
2、创建jsp页面确保form标签中有 enctype="multipart/form-data"属性
<form action="upload.action" method="post" enctype="multipart/form-data"
name="form1" id="form1">
<p>
照片1:
<input type="file" name="photo" id="fileField" />
</p>
<p>
照片2:
<input type="file" name="photo" id="fileField" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
</p>
</form>


3、编写文件上传处理Action
public class MultiAction extends ActionSupport {
private String userName;
// 封装上传文件属性
private File[] photo;
// 获取提交的文件类型
private String[] photoContentType;
// 封装上传文件名称
private String[] photoFileName;
//有struts.xml中通过param标签指定的上传路径
private String uploadPath;

@Override
public String execute() throws Exception {
byte[] buffer = new byte[1024];
for (int i = 0; i < photo.length; i++) {
FileInputStream fis = new FileInputStream(this.getPhoto()[i]);


String path = ServletActionContext.getServletContext().getRealPath(this.getUploadPath());
FileOutputStream fos = new FileOutputStream(path + "\\"
+ this.getPhotoFileName()[i]);
int length = fis.read(buffer);
while (length > 0) {
// 每次写入length长度的内容
fos.write(buffer, 0, length);
length = fis.read(buffer);
}
fis.close();
fos.flush();
fos.close();
}


return SUCCESS;
}


public String getUserName() {
return userName;
}


public void setUserName(String userName) {
this.userName = userName;
}


public File[] getPhoto() {
return photo;
}


public void setPhoto(File[] photo) {
this.photo = photo;
}


public String[] getPhotoContentType() {
return photoContentType;
}


public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
}


public String[] getPhotoFileName() {
return photoFileName;
}


public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String getUploadPath() {
return uploadPath;
}


public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
}


4、在struts.xml中配置Action
<action name="multi" class="action.MultiAction">
<!-- 通过设置action默认参数指定上传路径 -->
<param name="uploadPath">/shangchuan</param>
<result>showmulti.jsp</result>
</action>


############################文件下载##################################
1、创建一个能够支持下载的Action
public class DownloadAction extends ActionSupport {
// // 读取下载文件的目录
private String inputPath;
// 下载文件的文件名
private String fileName;
// 读取下载文件的输入流
private InputStream inputStream;
// 下载文件的类型
private String conetntType;


public InputStream getInputStream() throws FileNotFoundException {
String path = ServletActionContext.getServletContext().getRealPath(
this.getInputPath());
return new BufferedInputStream(new FileInputStream(path + "\\"
+ this.getFileName()));
}


@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "success";
}


public String getInputPath() {
return inputPath;
}


public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}


public String getFileName() {
return fileName;
}


public void setFileName(String fileName) {
this.fileName = fileName;
}


public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}


public String getConetntType() {
return conetntType;
}


public void setConetntType(String conetntType) {
this.conetntType = conetntType;
}


}
2、修改struts.xml中对应action标签里的内容
<action name="download" class="action.DownloadAction">
<!-- 通过设置action默认参数指定上传路径 -->
<param name="inputPath">/xiazai</param>
<result type="stream">
<!--设置要下载的文件类型:application/octet-stream:二进制文件-->
<param name="contentType">application/octet-stream</param>
<!--指定action中提供输入流的属性名称,自动去找getInputStream()-->
<param name="inputName">inputStream</param>
<!--设置http响应头,调用getFileName()方法-->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!--设置缓冲区大小-->
<param name="bufferSize">4096</param>
</result>
</action>


3、在jsp页面中通过调用action的方式下载文件
<a href="download.action?fileName=png-0001.png">下载</a>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值