Java上传文件,图片以及下载功能
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;
}
}
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;
}
}
...全文
相关推荐
java实现文件上传、下载、图片预览
主要介绍了java实现文件上传、下载、图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java的上传下载
一个简单的基于前后端的图片上传和文件下载功能的实现
spring boot实现图片上传和下载功能
主要为大家详细介绍了spring boot实现图片上传和下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java实现简单图片上传下载功能
主要为大家详细介绍了java实现简单图片上传下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java实现文件上传下载和图片压缩代码示例
本文给大家介绍的是项目中经常需要用到的一个常用的功能,使用java实现文件的上传下载和图片的压缩功能,这里推荐给大家,有需要的小伙伴参考下。


Web 开发
