Java上传文件,图片以及下载功能

詠聖wK 詠聖wK 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package org.wysheng.dadagame.web.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.wysheng.dadagame.domain.Game;
import org.wysheng.dadagame.service.GameSevice;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class DownLoadAction extends ActionSupport {

@Autowired
private GameSevice gameSevice;

@Override
public String execute() throws Exception {



Game game = this.gameSevice.showGameDetailById(gameid);
String downloadPath = game.getSite();
// 获得下载路径对应的文件的后缀
String extension = downloadPath.substring(downloadPath.lastIndexOf("."));

HttpServletResponse response = ServletActionContext.getResponse();
// 设置响应的方式(下载的方式)
response.setContentType("application/x-download");
// 获得游戏下载的默认名称(已游戏名称为下载文件默认名)
String filedisplay =game.getGamename();
// 如果下载的文件为中文,进行编码
String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
// 设置浏览器下载对话框的信息(以下载方式打开,设置下载默认类型和下载名称)
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay + extension);
// http请求的输出流
OutputStream outputStream = response.getOutputStream();
// 把对应下载的路径文件设置到输出流
InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream(downloadPath); // download/Constellation.7z
IOUtils.copy(inputStream, outputStream);
outputStream.close();
return null;
}
// 游戏编号
private String gameid;

public String getGameid() {
return gameid;
}
public void setGameid(String gameid) {
this.gameid = gameid;
}

}




//下载
package org.wysheng.dadagame.web.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.wysheng.dadagame.domain.Game;
import org.wysheng.dadagame.service.GameSevice;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class DownLoadAction extends ActionSupport {

@Autowired
private GameSevice gameSevice;

@Override
public String execute() throws Exception {



Game game = this.gameSevice.showGameDetailById(gameid);
String downloadPath = game.getSite();
// 获得下载路径对应的文件的后缀
String extension = downloadPath.substring(downloadPath.lastIndexOf("."));

HttpServletResponse response = ServletActionContext.getResponse();
// 设置响应的方式(下载的方式)
response.setContentType("application/x-download");
// 获得游戏下载的默认名称(已游戏名称为下载文件默认名)
String filedisplay =game.getGamename();
// 如果下载的文件为中文,进行编码
String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
// 设置浏览器下载对话框的信息(以下载方式打开,设置下载默认类型和下载名称)
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay + extension);
// http请求的输出流
OutputStream outputStream = response.getOutputStream();
// 把对应下载的路径文件设置到输出流
InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream(downloadPath); // download/Constellation.7z
IOUtils.copy(inputStream, outputStream);
outputStream.close();
return null;
}
// 游戏编号
private String gameid;

public String getGameid() {
return gameid;
}
public void setGameid(String gameid) {
this.gameid = gameid;
}

}
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
詠聖wK
Web 开发 81,108 成员 341,721 内容