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;
}

}
...全文
484 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
詠聖wK 2017-04-24
  • 打赏
  • 举报
回复
引用 4 楼 jiajing1990_ 的回复:
我很仔细的看了 没发现你要问什么 然后默默的走了
谢谢这位大神,我还不会用csdn,就想着随便写个帖子,记个笔记
詠聖wK 2017-04-24
  • 打赏
  • 举报
回复
引用 3 楼 hc275854577 的回复:
这是准备开发游戏吗
没有啊 ,自己随便练练手
詠聖wK 2017-04-24
  • 打赏
  • 举报
回复
引用 2 楼 pany1209 的回复:
骑士老詹球衣
莫不成这位兄台也喜欢我家老詹?
jiajing1990_ 2017-02-24
  • 打赏
  • 举报
回复
我很仔细的看了 没发现你要问什么 然后默默的走了
hc275854577 2017-02-24
  • 打赏
  • 举报
回复
这是准备开发游戏吗
李德胜1995 2017-02-24
  • 打赏
  • 举报
回复
骑士老詹球衣
Defonds 2017-02-24
  • 打赏
  • 举报
回复
技术共享可以去 csdn 博客频道 http://blog.csdn.net/

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧