81,095
社区成员
发帖
与我相关
我的任务
分享
<a href="downLoad.action?downPath=s.doc">
<action name="downLoad" class="action.DownLoadAction">
<result type="stream">
<param name="contentType">${contentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
public InputStream getInputStream() {
return ServletActionContext.getServletContext().getResourceAsStream(downPath);
}
public String execute() {
// 下载保存时的文件名和被下载的文件名一样
filename = downPath;
// 下载的文件路径
// downPath = "images/" + downPath;
// 保存文件的类型
contentType = "application/x-msdownload";
System.out.println(getInputStream());
// 对下载的文件名按照UTF-8进行编码,解决下载窗口中的中文乱码问题
filename = MyUtil.toUTF8String(filename);
return SUCCESS;
}
spring中
1.图片
<img src="**/showImg?path=url" >
@RequestMapping(value = "showImg")
public void showImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
String filePath = request.getParameter("path");
// String filePath="D:\1.jpg";
OutputStream output = null;
InputStream imageIn = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
output = response.getOutputStream();
response.setContentType("image/gif;charset=GB2312");
// 得到图片的文件流
imageIn = new FileInputStream(new File(filePath));
// 文件流
bis = new BufferedInputStream(imageIn);
// 输入缓冲流
bos = new BufferedOutputStream(output);
// 输出缓冲流
byte data[] = new byte[4096];
int size = 0;
size = bis.read(data);
while (size != -1) {
bos.write(data, 0, size);
size = bis.read(data);
}
bos.flush();
}catch(Exception e){
}
}
2.如果是文件
<a href="**.action?path=url">文件下载</a>
直接写入到输出流中
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition","attachment;filename=1.txt"));
//获取文件输入流
InputStream in = new FileInputStream(realPath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
//将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
in.close();
大概这样了,不全。注意你存储的是相对路径还是绝对路径,然后自己处理路径与文件名就好了。[/quote]
多谢大神,我结贴了,实在麻烦你了
<a href="downLoad.action?downPath=s.doc">
<action name="downLoad" class="action.DownLoadAction">
<result type="stream">
<param name="contentType">${contentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${filename}</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
public InputStream getInputStream() {
return ServletActionContext.getServletContext().getResourceAsStream(downPath);
}
public String execute() {
// 下载保存时的文件名和被下载的文件名一样
filename = downPath;
// 下载的文件路径
// downPath = "images/" + downPath;
// 保存文件的类型
contentType = "application/x-msdownload";
System.out.println(getInputStream());
// 对下载的文件名按照UTF-8进行编码,解决下载窗口中的中文乱码问题
filename = MyUtil.toUTF8String(filename);
return SUCCESS;
}
spring中
1.图片
<img src="**/showImg?path=url" >
@RequestMapping(value = "showImg")
public void showImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
String filePath = request.getParameter("path");
// String filePath="D:\1.jpg";
OutputStream output = null;
InputStream imageIn = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
output = response.getOutputStream();
response.setContentType("image/gif;charset=GB2312");
// 得到图片的文件流
imageIn = new FileInputStream(new File(filePath));
// 文件流
bis = new BufferedInputStream(imageIn);
// 输入缓冲流
bos = new BufferedOutputStream(output);
// 输出缓冲流
byte data[] = new byte[4096];
int size = 0;
size = bis.read(data);
while (size != -1) {
bos.write(data, 0, size);
size = bis.read(data);
}
bos.flush();
}catch(Exception e){
}
}
2.如果是文件
<a href="**.action?path=url">文件下载</a>
直接写入到输出流中
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition","attachment;filename=1.txt"));
//获取文件输入流
InputStream in = new FileInputStream(realPath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
//将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
in.close();
大概这样了,不全。注意你存储的是相对路径还是绝对路径,然后自己处理路径与文件名就好了。 <s:form action="uploadSingle" method="post"
enctype="multipart/form-data">
<input type="file" name="upload">
<input type="submit" value="开始上传">
</s:form>
@ParentPackage("struts-default")
@Action(value = "error", exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "common/errorPage.jsp", params = {"error", "value" }) })
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 3536238274545006462L;
// input
private File upload; // 封装上传的文件 临时保存的
private String uploadContentType; // 获得用户上传文件的类型
private String uploadFileName; // 获得用户上传文件的名字
private List<File> uploads = new ArrayList<File>(); // 使用集合来进行封装多个文件上传
private List<String> uploadsContentType = new ArrayList<String>();
private List<String> uploadsFileName = new ArrayList<String>();
// output
private String serverFileName; // 服务器保存的名字
private long uploadFileSize;
private List<String> serverFileNames = new ArrayList<String>();
private List<Long> uploadsFileSizes = new ArrayList<Long>();
// service
UploadService uploadService;
Category category;
ChildCategory childCategory;
@Actions(value = { @Action(value = "uploadSingle", results = { @Result(name = "success", location = "common/success.jsp")
,@Result(name = "error", location = "common/errorPage.jsp")}, interceptorRefs = {
@InterceptorRef(value = "fileUpload", params = { "allowedTypes","text/plain,image/x-png,image/bmp,image/jpeg","maximumSize", "2097152" }),
@InterceptorRef("defaultStack") },
exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "error", params = {
"error", "value" }) })}
)
public String uploadSingle() {
// 从ServletContext().getReaPath("/upload")中获取系统部署的实际路径
// /upload为自定义的文件夹,必须提前创建
String serverPath = ServletActionContext.getServletContext()
.getRealPath("/files/download");
// 建立文件上传的完整路径,File.separator获取不同系统的'/'
serverFileName = serverPath + File.separator + uploadFileName;
try {
copyFile(upload, serverFileName); // 把文件复制到实体硬盘
uploadFileSize = upload.length();
// 保存文件信息到数据库
fileSource.setFileName(uploadFileName);
fileSource.setFileUrl(serverPath);
fileSource.setFileSize(uploadFileSize);
fileSource.setUploadDate(new Date());
fileSource.setUploadUser("test");
uploadService.saveFileInfo(fileSource);
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return SUCCESS;
}
public UploadService getUploadService() {
return uploadService;
}
public Category getCategory() {
return category;
}
public FileSource getFileSource() {
return fileSource;
}
public String uploadsUsingList() {
String result = null;
for (int i = 0; i < uploads.size(); i++) {
upload = uploads.get(i);
uploadFileName = uploadsFileName.get(i);
result = uploadSingle();
uploadsFileSizes.add(uploadFileSize);
serverFileNames.add(serverFileName);
}
return result;
}
private void copyFile(File src, String dest) throws Exception {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
src));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(dest)));
int count; // 每次写入文件的大小
byte[] b = new byte[512]; // 每次尽可能读取的大小
while ((count = in.read(b)) != -1) {
out.write(b, 0, count);
}
in.close();
out.close();
}
}
这东西比较简单吧,百度应该一大把,就是一个文件流的操作,和保存一个和文件信息有关的对象