Java上传文件,图片以及下载功能
詠聖wK 2017-02-24 11:46:27 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;
}
}