因为安全性问题,需要提示用户下载
setContentType()
setContentType()
解决方案 »
- JSP 用Ajax技术怎么实现分页?
- 有用java将json转化为Table的函数不? 怎么弄的? 要引用什么?
- 求正则表达式
- hibernate update的问题
- jsp中,javascript代码怎么控制java代码呢?
- WebService与SOAP怎么结合起来?
- 想瞭解APPFUSE框架的使用問題
- 请问下面这个StringBufferInputStream有什么新的方法对应嘛?
- 版主,各位大侠,救救我吧 !eclipse开发j2ee初级问题
- 老板同意买书了,大家谈一谈EJB方面那一本书通俗易懂
- Jbuilder中如何生成可执行文件?
- eclipse编辑jsp文件时为什么字体的颜色没有高亮显示。
import java.net.*;
public class webFileRead {
public webFileRead() {
}
/**
* 把一个网络文件写到当前机器上
* @param String urlStr, 网络文件路径
* @param String filepath, 当前机器保存的完整路径
* @return boolean, 是否操作成功,返回true,操作成功
*/
public static boolean write( String urlStr, String filepath ) {
try
{
URL url = new URL( urlStr );
DataInputStream is = new DataInputStream( new BufferedInputStream( url.openStream() ) );
DataOutputStream os = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( filepath ) ) ); // 读写文件
byte b[] = new byte[1024];
int len = 0;
while ( ( len = is.read(b, 0, 1024 ) ) != -1)
{
os.write( b, 0, len );
} is.close();
os.flush();
os.close();
return true;
}
catch (MalformedURLException e)
{
System.out.println( "输入的网址错误!" + urlStr + "\r\n" + e.getMessage() );
return false;
}
catch (FileNotFoundException e)
{
System.out.println( "文件不存在错误!" + filepath );
return false;
}
catch ( IOException e )
{
System.out.println( "I/O出错!" + e.getMessage() );
return false;
}
} /* 不管什么文件都可以.
JSP中的用法
...write("http://www.168xs.com", request.getRealPath("/index.htm"));*/
public static void main(String args[]){
webFileRead wfr = new webFileRead();
wfr.write("http://www.sina.com.cn","c:/temp/testWebFile.shtml");
if("1".equals("1")){
System.out.println("1");
}else{ }
}}
try
{
File thePath = new File("C:\\p\\pra\\"+filepath);
if (!thePath.exists()) {
thePath.mkdirs();//建立文件夹,如果不存在的话
} String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.txt";//你的文件名
File theFile=new File(fullPath);
if (theFile.exists()) {
theFile.delete();//删除原有文件,写新文件
}
theFile.createNewFile();
RandomAccessFile fout = new RandomAccessFile(fullPath, "rw");
InputStream is;//这地方我没有初始化,你必须把InputStream传过来
byte by[]=new byte[is.available()];
is.read(by);
fout.write(by);
fout.close(); }catch(Exception e)
{
e.printStackTrace();
}
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
dboperate ds = new dboperate();
response.setContentType(CONTENT_TYPE);
// PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true); String d_safeguard_id = null;
d_safeguard_id = request.getParameter("d_safeguard_id"); String sql = "select * from d_safeguard where
d_safeguard_id="+d_safeguard_id;
ResultSet rs = ds.create_rs(sql);
rs.next();
//输入img字段内容到is
InputStream is = rs.getBinaryStream("photo");
response.reset();
String filepath="2004-3-30";
File thePath = new File("C:\\p\\pra\\"+filepath);
if (!thePath.exists()) {
thePath.mkdirs();//建立文件夹,如果不存在的话
}
String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.jpg";//你的文件名
File theFile=new File(fullPath);
if (theFile.exists()) {
theFile.delete();//删除原有文件,写新文件
}
theFile.createNewFile();
RandomAccessFile fout = new RandomAccessFile(fullPath, "rw");
byte by[]=new byte[is.available()];
int len;
while((len=is.read(by)) >0)
response.getOutputStream().write(by,0,len);
//is.read(by);
//fout.write(by);
//fout.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
我上面这段程序运行起来不报错,但是不能把blob流写进文件。谁知道是什么原因呢?
这个字段里有东西么?File thePath = new File("C:\\p\\pra\\"+filepath);
这地方这个路径你不要用C:,因为你用的是Servlet,它不一定对C盘有权限,你可以把文件夹建到你的Webapp中。